bench_strtr_str_replace.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. require_once __DIR__ . '/Benchmarker.php';
  26. function bfNormalizeTitleStrTr( $str ) {
  27. return strtr( $str, '_', ' ' );
  28. }
  29. function bfNormalizeTitleStrReplace( $str ) {
  30. return str_replace( '_', ' ', $str );
  31. }
  32. /**
  33. * Maintenance script that benchmarks for strtr() vs str_replace().
  34. *
  35. * @ingroup Benchmark
  36. */
  37. class BenchStrtrStrReplace extends Benchmarker {
  38. public function __construct() {
  39. parent::__construct();
  40. $this->addDescription( 'Benchmark for strtr() vs str_replace().' );
  41. }
  42. public function execute() {
  43. $this->bench( [
  44. [ 'function' => [ $this, 'benchstrtr' ] ],
  45. [ 'function' => [ $this, 'benchstr_replace' ] ],
  46. [ 'function' => [ $this, 'benchstrtr_indirect' ] ],
  47. [ 'function' => [ $this, 'benchstr_replace_indirect' ] ],
  48. ] );
  49. print $this->getFormattedResults();
  50. }
  51. function benchstrtr() {
  52. strtr( "[[MediaWiki:Some_random_test_page]]", "_", " " );
  53. }
  54. function benchstr_replace() {
  55. str_replace( "_", " ", "[[MediaWiki:Some_random_test_page]]" );
  56. }
  57. function benchstrtr_indirect() {
  58. bfNormalizeTitleStrTr( "[[MediaWiki:Some_random_test_page]]" );
  59. }
  60. function benchstr_replace_indirect() {
  61. bfNormalizeTitleStrReplace( "[[MediaWiki:Some_random_test_page]]" );
  62. }
  63. }
  64. $maintClass = 'BenchStrtrStrReplace';
  65. require_once RUN_MAINTENANCE_IF_MAIN;