Introduction to PEAR - The Benchmark Package
(Page 3 of 4 )
The Benchmark package is now installed and ready to go. The desired extensions are made available to our script by using the require_once statement to include the appropriate file.
<?php require_once "Benchmark/Timer.php"; ?> |
If PHP complains it was unable find the file, you may need to modify your php.ini file's include_path directive. Or, you can temporarily append the directory to the include path for the duration of the script by using the ini_set() function.
<?php ini_set("include_path", ini_get("include_path") . "/usr/local/lib/php"); require_once "Benchmark/Timer.php"; ?> |
The Benchmark module times the execution of a script and displays the results in the form of a table.
<?php require_once "Benchmark/Timer.php";
$t = new Benchmark_Timer(); $t->start(); echo "Hello, World! <br />"; $t->stop();
echo "<pre>" . $t->display() . "</pre>"; ?> |
A new timer object is created with new Benchmark_Timer and is made accessible by assigning it to the variable $t. The object can then be manipulated by the methods start, stop and display. Intuitively, start will start the timer, stop will stop the timer and display will output the results table.
The output of the above script would resemble the following:
Hello World! ------------------------------------------------------------- marker time index ex time perct ------------------------------------------------------------- Start 1064782761.34820600 - 0.00% ------------------------------------------------------------- Stop 1064782761.34845500 0.00024890899658203 100.00% ------------------------------------------------------------- total - 0.00024890899658203 100.00% ------------------------------------------------------------- |
You may even set markers throughout your code to compare the execution time of various sections. This is done with the setMarker method which accepts a string to identify the marker.
<?php require_once "Benchmark/Timer.php";
$t = new Benchmark_Timer(); $t->start(); echo "Hello, World! <br />";
$t->setMarker("Section One"); for ($count = 0; $count < 100; $count++) { echo $count+1 . " "; } echo "<br />";
$t->setMarker("Section Two"); echo "Good Bye! <br />";
$t->stop();
echo "<pre>" . $t->display() . "</pre>"; ?> |
The markers create new entries in the final results table.
Hello, World! 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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 Good Bye! ------------------------------------------------------------------- marker time index ex time perct ------------------------------------------------------------------- Start 1064783416.23436900 - 0.00% ------------------------------------------------------------------- Section One 1064783416.23462200 0.00025296211242676 6.04% ------------------------------------------------------------------- Section Two 1064783416.23846400 0.0038419961929321 91.80% ------------------------------------------------------------------- Stop 1064783416.23855400 9.000301361084E-05 2.15% ------------------------------------------------------------------- total - 0.0041849613189697 100.00% ------------------------------------------------------------------- |
The benchmarking utility provided through PEAR is easy to implement and is a useful tool to examine the efficiency of your scripts... but it's also only one of many useful PEAR packages. To date over 200 packages are available through the PEAR Package Manager and are organized in various repositories.
Next: Conclusion >>
More PEAR Articles Articles
More By bluephoenix