r79854 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r79853‎ | r79854 | r79855 >
Date:01:10, 8 January 2011
Author:brion
Status:ok
Tags:
Comment:
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)
Modified paths:
  • /trunk/phase3/maintenance/benchmarks/bench_strtr_str_replace.php (modified) (history)

Diff [purge]

Index: trunk/phase3/maintenance/benchmarks/bench_strtr_str_replace.php
@@ -1,6 +1,15 @@
22 <?php
33
44 require_once( dirname( __FILE__ ) . '/Benchmarker.php' );
 5+
 6+function bfNormalizeTitleStrTr( $str ) {
 7+ return strtr( $str, '_', ' ' );
 8+}
 9+
 10+function bfNormalizeTitleStrReplace( $str ) {
 11+ return str_replace( '_', ' ', $str );
 12+}
 13+
514 class bench_strtr_str_replace extends Benchmarker {
615
716 public function __construct() {
@@ -12,18 +21,29 @@
1322 $this->bench( array(
1423 array( 'function' => array( $this, 'benchstrtr' ) ),
1524 array( 'function' => array( $this, 'benchstr_replace' ) ),
 25+ array( 'function' => array( $this, 'benchstrtr_indirect' ) ),
 26+ array( 'function' => array( $this, 'benchstr_replace_indirect' ) ),
1627 ));
1728 print $this->getFormattedResults();
1829 }
1930
2031 function benchstrtr() {
21 - strtr( "[[MediaWiki:Some_random_test_page]]", "_", "" );
 32+ strtr( "[[MediaWiki:Some_random_test_page]]", "_", " " );
2233 }
2334
2435 function benchstr_replace() {
2536 str_replace( "_", " ", "[[MediaWiki:Some_random_test_page]]");
2637 }
2738
 39+
 40+ function benchstrtr_indirect() {
 41+ bfNormalizeTitleStrTr( "[[MediaWiki:Some_random_test_page]]" );
 42+ }
 43+
 44+ function benchstr_replace_indirect() {
 45+ bfNormalizeTitleStrReplace( "[[MediaWiki:Some_random_test_page]]" );
 46+ }
 47+
2848 }
2949
3050 $maintClass = 'bench_strtr_str_replace';

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r79755Add benchmark for bug 26605 Use strtr instead of str_replace when possiblereedy21:00, 6 January 2011

Status & tagging log