Fix & additions for strtr vs str_replace benchmarks (for bug 26605 discussion)
authorBrion Vibber <brion@users.mediawiki.org>
Sat, 8 Jan 2011 01:10:42 +0000 (01:10 +0000)
committerBrion Vibber <brion@users.mediawiki.org>
Sat, 8 Jan 2011 01:10:42 +0000 (01:10 +0000)
commitfabdcfb78a50be7c44cf4c8ca55afe81a026a218
treef24f672480181e878ff6c0e0f93fa416d03b5ce9
parent772888860423fdf12002968e508fd5e5c2218b70
Fix & additions for strtr vs str_replace benchmarks (for bug 26605 discussion)

The strtr function in the benchmark was slightly artificially accelerated by replacing '_' with '' (empty) instead of ' ' (space), making the output string shorter and thus processing faster. This is now fixed, which makes the results slightly closer.
I also added 'indirect' functions to the benchmark, which instead of calling strtr/str_replace directly, call a global function which then calls them (equivalent to what we'd do if we turned the replaces into wf* global functions)

strtr() does appear to be slightly faster than str_replace() for this workload, but it's fairly modest and approximately the same as the overhead of a function call.

Results on my test box (MacBook Pro, 2.4GHz Core 2 Duo, Mac OS X 10.6.6, 64-bit PHP 5.3.4 built via MacPorts) with 10,000 reps:

$ php bench_strtr_str_replace.php --count=10000
10000 times: function bench_strtr_str_replace->benchstrtr() :
    19.67ms (  0.00ms each)
10000 times: function bench_strtr_str_replace->benchstr_replace() :
    22.05ms (  0.00ms each)
10000 times: function bench_strtr_str_replace->benchstrtr_indirect() :
    22.53ms (  0.00ms each)
10000 times: function bench_strtr_str_replace->benchstr_replace_indirect() :
    26.29ms (  0.00ms each)
maintenance/benchmarks/bench_strtr_str_replace.php