Merge "Fix API output formatting (change lines delimited with * as bold)"
[lhc/web/wiklou.git] / maintenance / benchmarks / bench_strtr_str_replace.php
1 <?php
2 /**
3 * Benchmark for strtr() vs str_replace().
4 *
5 * This come from r75429 message.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 * @ingroup Benchmark
24 */
25
26 require_once( __DIR__ . '/Benchmarker.php' );
27
28 function bfNormalizeTitleStrTr( $str ) {
29 return strtr( $str, '_', ' ' );
30 }
31
32 function bfNormalizeTitleStrReplace( $str ) {
33 return str_replace( '_', ' ', $str );
34 }
35
36 /**
37 * Maintenance script that benchmarks for strtr() vs str_replace().
38 *
39 * @ingroup Benchmark
40 */
41 class bench_strtr_str_replace extends Benchmarker {
42
43 public function __construct() {
44 parent::__construct();
45 $this->mDescription = "Benchmark for strtr() vs str_replace().";
46 }
47
48 public function execute() {
49 $this->bench( array(
50 array( 'function' => array( $this, 'benchstrtr' ) ),
51 array( 'function' => array( $this, 'benchstr_replace' ) ),
52 array( 'function' => array( $this, 'benchstrtr_indirect' ) ),
53 array( 'function' => array( $this, 'benchstr_replace_indirect' ) ),
54 ));
55 print $this->getFormattedResults();
56 }
57
58 function benchstrtr() {
59 strtr( "[[MediaWiki:Some_random_test_page]]", "_", " " );
60 }
61
62 function benchstr_replace() {
63 str_replace( "_", " ", "[[MediaWiki:Some_random_test_page]]");
64 }
65
66
67 function benchstrtr_indirect() {
68 bfNormalizeTitleStrTr( "[[MediaWiki:Some_random_test_page]]" );
69 }
70
71 function benchstr_replace_indirect() {
72 bfNormalizeTitleStrReplace( "[[MediaWiki:Some_random_test_page]]" );
73 }
74
75 }
76
77 $maintClass = 'bench_strtr_str_replace';
78 require_once( RUN_MAINTENANCE_IF_MAIN );