]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/benchmarks/bench_strtr_str_replace.php
MediaWiki 1.30.2
[autoinstalls/mediawiki.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 BenchStrtrStrReplace extends Benchmarker {
42         public function __construct() {
43                 parent::__construct();
44                 $this->addDescription( 'Benchmark for strtr() vs str_replace().' );
45         }
46
47         public function execute() {
48                 $this->bench( [
49                         [ 'function' => [ $this, 'benchstrtr' ] ],
50                         [ 'function' => [ $this, 'benchstr_replace' ] ],
51                         [ 'function' => [ $this, 'benchstrtr_indirect' ] ],
52                         [ 'function' => [ $this, 'benchstr_replace_indirect' ] ],
53                 ] );
54         }
55
56         protected function benchstrtr() {
57                 strtr( "[[MediaWiki:Some_random_test_page]]", "_", " " );
58         }
59
60         protected function benchstr_replace() {
61                 str_replace( "_", " ", "[[MediaWiki:Some_random_test_page]]" );
62         }
63
64         protected function benchstrtr_indirect() {
65                 bfNormalizeTitleStrTr( "[[MediaWiki:Some_random_test_page]]" );
66         }
67
68         protected function benchstr_replace_indirect() {
69                 bfNormalizeTitleStrReplace( "[[MediaWiki:Some_random_test_page]]" );
70         }
71 }
72
73 $maintClass = 'BenchStrtrStrReplace';
74 require_once RUN_MAINTENANCE_IF_MAIN;