Recipe 21.1. Timing Function Execution


21.1.1. Problem

You have a function and you want to see how long it takes to execute.

21.1.2. Solution

Compare time in milliseconds before running the function against the time in milliseconds after running the function to see the elapsed time spent in the function itself:

<?php // create a long nonsense string $long_str = uniqid(php_uname('a'), true); // start timing from here $start = microtime(true); // function to test $md5 = md5($long_str); $elapsed = microtime(true) - $start; echo "That took $elapsed seconds.\n"; ?>

21.1.3. Discussion

To determine how much time a single function takes to execute, you may not need a full benchmarking package like PEAR Benchmark (which is covered in Recipe 21.2). Instead, you can get the information you need from the microtime( ) function.

21.1.4. See Also

Using microtime( ), including how it works in PHP 4, is discussed in Recipe 3.13; documentation on the microtime( ) function is at http://www.php.net/microtime; Recipe 21.2 looks at PEAR Benchmark.




PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net