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 Section in PHP
PHP

Timing Program Execution by Section in PHP

InfinityCoder December 24, 2016

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

Use the PEAR Benchmark module:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
require_once 'Benchmark/Timer.php';i
 
$timer = new Benchmark_Timer(true);
 
$timer->start();
// some setup code here
$timer->setMarker('setup');
// some more code executed here
$timer->setMarker('middle');
// even yet still more code here
$timer->setmarker('done');
// and a last bit of code here
$timer->stop();
 
$timer->display();

The PEAR Benchmark package gives you a quick-and-dirty way to set a few markers in your code to identify hotspots at a more macro level.

Install it using the PEAR package manager:

1
% pear install Benchmark

Calling setMarker() records the time.

The display() method prints out a list of markers, the time they were set, and the elapsed time from the previous marker:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
-------------------------------------------------------------
marker   time index            ex time                 perct
-------------------------------------------------------------
Start    1029433375.42507400   -                       0.00%
-------------------------------------------------------------
setup    1029433375.42554800   0.00047397613525391    29.77%
-------------------------------------------------------------
middle   1029433375.42568700   0.00013899803161621     8.73%
-------------------------------------------------------------
done     1029433375.42582000   0.00013303756713867     8.36%
-------------------------------------------------------------
Stop     1029433375.42666600   0.00084602832794189     53.14%
-------------------------------------------------------------
total    -                     0.0015920400619507     100.00%
-------------------------------------------------------------

The Benchmark module also includes the Benchmark_Iterate class, which can be used to time many executions of a single function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
require 'Benchmark/Iterate.php';
 
$timer = new Benchmark_Iterate;
 
// a sample function to time
function use_preg($ar) {
   for ($i = 0, $j = count($ar); $i < $j; $i++) {
      if (preg_match('/gouda/',$ar[$i])) {
          // it's gouda
      }
   }
}
 
// another sample function to time
function use_equals($ar) {
   for ($i = 0, $j = count($ar); $i < $j; $i++) {
      if ('gouda' == $ar[$i]) {
          // it's gouda
      }
   }
}
 
// run use_preg() 1000 times
$timer->run(1000,'use_preg',
                  array('gouda','swiss','gruyere','muenster','whiz'));
$results = $timer->get();
print "Mean execution time for use_preg(): $results[mean]\n";
 
// run use_equals() 1000 times
$timer->run(1000,'use_equals',
                  array('gouda','swiss','gruyere','muenster','whiz'));
$results = $timer->get();
print "Mean execution time for use_equals(): $results[mean]\n";

The Benchmark_Iterate::get() method returns an associative array. The mean elementof this array holds the mean execution time for each iteration of the function.

The iterations element holds the number of iterations. The execution time of each iteration of the function is stored in an array element with an integer key.

For example, the time of the first iteration is in $results[1], and the time of the 37th iteration is in $results[37].

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Generating XML with DOM in PHP
You want to generate XML but want to do it …

Generating XML with DOM in PHP

Caching Queries and Results in PHP
You don’t want to rerun potentially expensive database queries when …

Caching Queries and Results 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