InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Timing Program Execution by Function, in PHP
PHP

Timing Program Execution by Function, in PHP

InfinityCoder December 24, 2016

You have a block of code and you want to profile it to see how long each function takes to execute.

Use Xdebug function tracing:

1
2
3
4
5
6
7
8
9
xdebug_start_trace('/tmp/factorial-trace');
 
function factorial($x) {
   return ($x == 1) ? 1 : $x * factorial($x - 1);
}
 
print factorial(10);
 
xdebug_stop_trace();

The Xdebug extension provides a wide range of helpful debugging and profiling features.
It’s available via PECL or as a prebuilt Windows binary.
Its function-tracing feature provides insight into what functions are called from where, optionally including the arguments passed and returned.

It also records the time and memory taken for each call.
For the factorial example, it generates results such as:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
TRACE START [2015-01-05 06:32:11]
   0.0005      240136   -> factorial($x = 10) /factorial.php:9
   0.0005      240184    -> factorial($x = 9) /factorial.php:6
   0.0005      240256     -> factorial($x = 8) /factorial.php:6
   0.0006      240304      -> factorial($x = 7) /factorial.php:6
   0.0006      240352       -> factorial($x = 6) /factorial.php:6
   0.0006      240400        -> factorial($x = 5) /factorial.php:6
   0.0007      240448         -> factorial($x = 4) /factorial.php:6
   0.0007      240496          -> factorial($x = 3) /factorial.php:6
   0.0007      240544           -> factorial($x = 2) /factorial.php:6
   0.0008      240592            -> factorial($x = 1) /factorial.php:6
                                  >=> 1
                                 >=> 2
                                >=> 6
                               >=> 24
                              >=> 120
                             >=> 720
                            >=> 5040
                           >=> 40320
                          >=> 362880
                         >=> 3628800
   0.0010     240136    -> xdebug_stop_trace() /factorial.php:12
   0.0010     240176
TRACE END [2015-01-05 06:32:11]

The first column lists the start time in seconds. The next column has the memory usage.
Then you see the function called, along with the argument passed. Finally, it lists the filename and line number.

As the functions execute, you see the data returned from each one.
Xdebug allows you to format these results in a number of ways. This specific output format uses the following configuration parameters:

1
2
3
xdebug.trace_format=0    ; human readable plain text
xdebug.collect_params=4  ; full variable contents and variable name.
xdebug.collect_return=1  ; show return values

Xdebug’s function profiling provides an easy way to get a detailed overview of everything going on in a section of code.

However, there are times when this may be more granular than you need.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Modifying a File in Place Without a Temporary File in PHP
You want to change a file without using a temporary …

Modifying a File in Place Without a Temporary File in PHP

Assigning Object References in PHP
You want to link two objects, so when you update …

Assigning Object References in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap