bench_if_switch.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Benchmark if elseif... versus switch case.
  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. * @author Platonides
  25. */
  26. require_once __DIR__ . '/Benchmarker.php';
  27. /**
  28. * Maintenance script that benchmark if elseif... versus switch case.
  29. *
  30. * @ingroup Maintenance
  31. */
  32. class BenchIfSwitch extends Benchmarker {
  33. public function __construct() {
  34. parent::__construct();
  35. $this->addDescription( 'Benchmark if elseif... versus switch case.' );
  36. }
  37. public function execute() {
  38. $this->bench( [
  39. [ 'function' => [ $this, 'doElseIf' ] ],
  40. [ 'function' => [ $this, 'doSwitch' ] ],
  41. ] );
  42. print $this->getFormattedResults();
  43. }
  44. // bench function 1
  45. function doElseIf() {
  46. $a = 'z';
  47. if ( $a == 'a' ) {
  48. } elseif ( $a == 'b' ) {
  49. } elseif ( $a == 'c' ) {
  50. } elseif ( $a == 'd' ) {
  51. } elseif ( $a == 'e' ) {
  52. } elseif ( $a == 'f' ) {
  53. } elseif ( $a == 'g' ) {
  54. } elseif ( $a == 'h' ) {
  55. } elseif ( $a == 'i' ) {
  56. } elseif ( $a == 'j' ) {
  57. } elseif ( $a == 'k' ) {
  58. } elseif ( $a == 'l' ) {
  59. } elseif ( $a == 'm' ) {
  60. } elseif ( $a == 'n' ) {
  61. } elseif ( $a == 'o' ) {
  62. } elseif ( $a == 'p' ) {
  63. } else {
  64. }
  65. }
  66. // bench function 2
  67. function doSwitch() {
  68. $a = 'z';
  69. switch ( $a ) {
  70. case 'b':
  71. break;
  72. case 'c':
  73. break;
  74. case 'd':
  75. break;
  76. case 'e':
  77. break;
  78. case 'f':
  79. break;
  80. case 'g':
  81. break;
  82. case 'h':
  83. break;
  84. case 'i':
  85. break;
  86. case 'j':
  87. break;
  88. case 'k':
  89. break;
  90. case 'l':
  91. break;
  92. case 'm':
  93. break;
  94. case 'n':
  95. break;
  96. case 'o':
  97. break;
  98. case 'p':
  99. break;
  100. default:
  101. }
  102. }
  103. }
  104. $maintClass = 'BenchIfSwitch';
  105. require_once RUN_MAINTENANCE_IF_MAIN;