PHP benchmark (was: Incremental parser)

Michel Fortin michel.fortin at michelf.com
Tue Sep 4 10:40:27 EDT 2007


Le 2007-09-03 à 0:02, Allan Odgaard a écrit :


> Ta-da! PHP is now faster than calling regexp stuff to do the same

> job (in majority of cases)! :)


Your modified test is very interesting. It shows the connexion
between the number of PHP objects (strings) created and the speed.

It doesn't change much my view though: the more you can do with a
regex and outside PHP code, the faster the code will be. You simply
tweaked the input conditions to show that in the worse case (where
every character has to be counted) the regex-based approaches
creating an array of the counted characters is slightly slower than
the plain PHP version.

That could be expected for two reasons: the most performant approach
using preg_match scans the input three times, increasing the
overhead, and it creates an array for the result which is more
expensive than just creating objects and storing them in local
variables (a PHP array is a linked list coupled with a hash table).

So I conclude that if you're going to create the same number of
objects, better do it in one pass and not put the result in an array.

But what's generally interesting in a regex-based approach is that
you often can avoid creating many of these objects. Take test number
8 for instance, which works in three passes but creates only 3 PHP
strings: it's still way faster than your improved pure-PHP approach.


Michel Fortin
michel.fortin at michelf.com
http://www.michelf.com/




More information about the Markdown-Discuss mailing list