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)
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

index 9a28886..565f3a1 100644 (file)
@@ -1,6 +1,15 @@
 <?php
 
 require_once( dirname( __FILE__ ) . '/Benchmarker.php' );
+
+function bfNormalizeTitleStrTr( $str ) {
+    return strtr( $str, '_', ' ' );
+}
+
+function bfNormalizeTitleStrReplace( $str ) {
+    return str_replace( '_', ' ', $str );
+}
+
 class bench_strtr_str_replace extends Benchmarker {
 
        public function __construct() {
@@ -12,18 +21,29 @@ class bench_strtr_str_replace extends Benchmarker {
                $this->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';