From fabdcfb78a50be7c44cf4c8ca55afe81a026a218 Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Sat, 8 Jan 2011 01:10:42 +0000 Subject: [PATCH] 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) --- .../benchmarks/bench_strtr_str_replace.php | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/maintenance/benchmarks/bench_strtr_str_replace.php b/maintenance/benchmarks/bench_strtr_str_replace.php index 9a28886387..565f3a1896 100644 --- a/maintenance/benchmarks/bench_strtr_str_replace.php +++ b/maintenance/benchmarks/bench_strtr_str_replace.php @@ -1,6 +1,15 @@ bench( array( array( 'function' => array( $this, 'benchstrtr' ) ), array( 'function' => array( $this, 'benchstr_replace' ) ), + array( 'function' => array( $this, 'benchstrtr_indirect' ) ), + array( 'function' => array( $this, 'benchstr_replace_indirect' ) ), )); print $this->getFormattedResults(); } function benchstrtr() { - strtr( "[[MediaWiki:Some_random_test_page]]", "_", "" ); + strtr( "[[MediaWiki:Some_random_test_page]]", "_", " " ); } function benchstr_replace() { str_replace( "_", " ", "[[MediaWiki:Some_random_test_page]]"); } + + function benchstrtr_indirect() { + bfNormalizeTitleStrTr( "[[MediaWiki:Some_random_test_page]]" ); + } + + function benchstr_replace_indirect() { + bfNormalizeTitleStrReplace( "[[MediaWiki:Some_random_test_page]]" ); + } + } $maintClass = 'bench_strtr_str_replace'; -- 2.20.1