PHP benchmark (was: Incremental parser)
29mtuz102 at sneakemail.com
Mon Sep 3 00:02:34 EDT 2007
On Aug 15, 2007, at 5:24 PM, Michel Fortin wrote:
This micro-benchmark gives us a lower running time for a parser that
relies on the constructs tested in the benchmark. So is the lower
running time really that bad? Well, I changed the premises slightly
just to show that the lower running time of the raw PHP is actually
better than most of your regexp solutions… ;)
> As a demonstration, I've made a small test program that counts
> characters 'a', 'b', and 'c' inside a string. There's nine
> different implementations counting those characters. No matter what
> the string size is (still within reasonable limits), the pure-regex
> implementation is always faster in my tests [...]
What you are testing here in the raw PHP version is creating some
200,000 objects (or more) since likely, a new string object is
created in each iteration of the loop.
The number of a’s, b’s, or c’s in the text should not affect
performance of the raw PHP code, and the text size should affect it
In the regexp case, the number of a’s, b’s, and c’s in the text does
affect the amount of work done, and even though this is all done in
C, w/o creating PHP objects, we can increase the work done here
simply by ensuring that more of the letters need to be worked on, so
I changed the text generating line to:
$text .= chr(rand(ord('a'), ord('c')));
And here are my numbers (I also changed the a case to use a while
loop instead of a for loop, it didn’t improve much, but some):
1) a: 66592; b: 66790; c:66617; time: 123 ms.
2) a: 66592; b: 66790; c:66617; time: 247 ms.
3) a: 66592; b: 66790; c:66617; time: 114 ms.
4) a: 66592; b: 66790; c:66617; time: 142 ms.
5) a: 66592; b: 66790; c:66617; time: 213 ms.
6) a: 66592; b: 66790; c:66617; time: 419 ms.
7) a: 66592; b: 66790; c:66617; time: 310 ms.
8) a: 66592; b: 66790; c:66617; time: 35 ms.
9) a: 66592; b: 66790; c:66617; time: 22 ms.
Ta-da! PHP is now faster than calling regexp stuff to do the same job
(in majority of cases)! :)
(benchmarks from PHP5 on a 2.33 GHz MBP)
More information about the Markdown-Discuss