I just wrote this recently and have been using it quite a lot. Whenever I have creative block I can go back and try to refactor things with it. It's not as robust as the Apache bench tool or anything, but I still find it handy:
// don't use this stupid function, it's just here to test function bbcodestr($text) { return(str_ireplace(array('[b]','[u]','[i]', '[/i][/u][/b][u][i]', '[/i][/u][i]', '[/i]'), array('<b>', '<u>','<i>', '</b>', '</u>', '</i>'), $text)); }
// this one either function bbcodepreg($text) { return(preg_replace(array('/\[([biu])\]/i', '/\[\/([biu])\]/i'), array('<$1>', '</$1>'), $text)); }
$teststring = '[b]what [u]is[/u] [i]up[/i][/b]?';
$speedtest = bench('bbcodestr(\'' . $teststring . '\');', 'bbcodepreg(\'' . $teststring . '\');', 10000); // run 10,000 times per trial, for the default 11 trials. Note the quotation marks and terminating semicolons are preserved in the function calls
//print_r($speedtest); // more detailed info foreach ($speedtest as $key=>$trial) { echo 'Trial ' . ($key + 1) . ' winner: ' . $trial['winner'] . ' by a factor of ' . $trial['ratio'] . '<br />'; }
/* Sample results, YMMV:
Trial 1 winner: bbcodepreg() by a factor of 1.0701883025352 Trial 2 winner: bbcodestr() by a factor of 1.1007553220986 Trial 3 winner: bbcodestr() by a factor of 1.1745991875412 Trial 4 winner: bbcodestr() by a factor of 1.1061013972411 Trial 5 winner: bbcodestr() by a factor of 1.1113493309722 Trial 6 winner: bbcodestr() by a factor of 1.0427249521624 Trial 7 winner: bbcodestr() by a factor of 1.0399104266645 Trial 8 winner: bbcodestr() by a factor of 1.0071057292584 Trial 9 winner: bbcodestr() by a factor of 1.1914867817959 Trial 10 winner: bbcodestr() by a factor of 1.0953776958654 Trial 11 winner: bbcodestr() by a factor of 1.0666147280889
The first result tends to be fishy and is usually discarded, hence the default of 11 trials. */
Try not to run this on shared hosting
Last edited by ralph l mayo; 12-11-2005 at 02:44 AM..
MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
Instead of microtime(), you might consider using getrusage(), which calculates the time actually used, rather than sitting around waiting for its turn in the CPU. It outputs the time both in system time and user time
cool, interesting use of eval.. which I thought may skew the results a little , and this can be seen in functions with marginal differences.
Yeah I was going to mention that eval() is slow as hell inside loops, and with most trivial functions nearly all the execution time will be eval related overhead, so this function is no good for testing how fast a function is, only for seeing how it compares to another.
I'm not sure if it'll really skew the results one way or another as both functions are subject to its overhead, but I don't know exactly how it's implemented and it's very possible that certain operations have a bigger footprint in an eval() context than in regular code.
Instead of microtime(), you might consider using getrusage(), which calculates the time actually used, rather than sitting around waiting for its turn in the CPU. It outputs the time both in system time and user time