3v4l.org

run code in 300+ PHP versions simultaneously
<?php //copyright Lawrence Truett and FluffyCat.com 2009, all rights reserved define('BR', "\n"); echo 'Performance Testing PHP if / else VS switch with 100,000 iterations'.BR.BR; $ifTime = 0; $switchTime = 0; $arrayTime = 0; for ($x = 0; $x < 100000; $x++) { $oneToFive = rand(1,5); if (0 == fmod($x,2)) { $switchTime = $switchTime + testSwitch($oneToFive); $arrayTime = $arrayTime + testArray($oneToFive); $ifTime = $ifTime + testIf($oneToFive); } else { $ifTime = $ifTime + testArray($oneToFive); $arrayTime = $arrayTime + testArray($oneToFive); $switchTime = $switchTime + testIf($oneToFive); } } echo 'total if / else time: '.$ifTime.BR; echo 'total case time: '.$switchTime.BR; echo 'total array time: '.$arrayTime.BR; echo BR; if ($ifTime > $switchTime) { echo 'switch is quicker by '.($ifTime - $switchTime).BR; } else { echo 'if / else is quicker by '.($switchTime - $ifTime).BR; } function testSwitch($oneToFive) { $time_start = microtime(true); switch($oneToFive) { case 1: $z = 1; break; case 2: $z = 2; break; case 3: $z = 3; break; case 4: $z = 4; break; case 5: $z = 5; break; } $time_end = microtime(true); $time = $time_end - $time_start; return $time; } function testIf($oneToFive) { $time_start = microtime(true); if (1 == $oneToFive) { $z = 1; } elseif(2 == $oneToFive) { $z = 2; } elseif(3 == $oneToFive) { $z = 3; } elseif(4 == $oneToFive) { $z = 4; } elseif(5 == $oneToFive) { $z = 5; } $time_end = microtime(true); $time = $time_end - $time_start; return $time; } function testArray($oneToFive){ $time_start = microtime(true); $array = array(0,1,2,3,4,5); $result = $array[$oneToFive]; $time_end = microtime(true); $time = $time_end - $time_start; return $time; }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 57
Branch analysis from position: 57
2 jumps found. (Code = 44) Position 1 = 59, Position 2 = 14
Branch analysis from position: 59
2 jumps found. (Code = 43) Position 1 = 75, Position 2 = 81
Branch analysis from position: 75
1 jumps found. (Code = 42) Position 1 = 86
Branch analysis from position: 86
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 81
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 41
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 56
Branch analysis from position: 56
2 jumps found. (Code = 44) Position 1 = 59, Position 2 = 14
Branch analysis from position: 59
Branch analysis from position: 14
Branch analysis from position: 41
2 jumps found. (Code = 44) Position 1 = 59, Position 2 = 14
Branch analysis from position: 59
Branch analysis from position: 14
filename:       /in/t8CM0
function name:  (null)
number of ops:  87
compiled vars:  !0 = $ifTime, !1 = $switchTime, !2 = $arrayTime, !3 = $x, !4 = $oneToFive
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   INIT_FCALL                                               'define'
          1        SEND_VAL                                                 'BR'
          2        SEND_VAL                                                 '%0A'
          3        DO_ICALL                                                 
    4     4        FETCH_CONSTANT                                   ~6      'BR'
          5        CONCAT                                           ~7      'Performance+Testing+PHP+if+%2F+else+VS+switch+with+100%2C000+iterations', ~6
          6        FETCH_CONSTANT                                   ~8      'BR'
          7        CONCAT                                           ~9      ~7, ~8
          8        ECHO                                                     ~9
    6     9        ASSIGN                                                   !0, 0
    7    10        ASSIGN                                                   !1, 0
    8    11        ASSIGN                                                   !2, 0
    9    12        ASSIGN                                                   !3, 0
         13      > JMP                                                      ->57
   10    14    >   INIT_FCALL                                               'rand'
         15        SEND_VAL                                                 1
         16        SEND_VAL                                                 5
         17        DO_ICALL                                         $14     
         18        ASSIGN                                                   !4, $14
   11    19        INIT_FCALL                                               'fmod'
         20        SEND_VAR                                                 !3
         21        SEND_VAL                                                 2
         22        DO_ICALL                                         $16     
         23        IS_EQUAL                                                 $16, 0
         24      > JMPZ                                                     ~17, ->41
   12    25    >   INIT_FCALL_BY_NAME                                       'testSwitch'
         26        SEND_VAR_EX                                              !4
         27        DO_FCALL                                      0  $18     
         28        ADD                                              ~19     !1, $18
         29        ASSIGN                                                   !1, ~19
   13    30        INIT_FCALL_BY_NAME                                       'testArray'
         31        SEND_VAR_EX                                              !4
         32        DO_FCALL                                      0  $21     
         33        ADD                                              ~22     !2, $21
         34        ASSIGN                                                   !2, ~22
   14    35        INIT_FCALL_BY_NAME                                       'testIf'
         36        SEND_VAR_EX                                              !4
         37        DO_FCALL                                      0  $24     
         38        ADD                                              ~25     !0, $24
         39        ASSIGN                                                   !0, ~25
   11    40      > JMP                                                      ->56
   16    41    >   INIT_FCALL_BY_NAME                                       'testArray'
         42        SEND_VAR_EX                                              !4
         43        DO_FCALL                                      0  $27     
         44        ADD                                              ~28     !0, $27
         45        ASSIGN                                                   !0, ~28
   17    46        INIT_FCALL_BY_NAME                                       'testArray'
         47        SEND_VAR_EX                                              !4
         48        DO_FCALL                                      0  $30     
         49        ADD                                              ~31     !2, $30
         50        ASSIGN                                                   !2, ~31
   18    51        INIT_FCALL_BY_NAME                                       'testIf'
         52        SEND_VAR_EX                                              !4
         53        DO_FCALL                                      0  $33     
         54        ADD                                              ~34     !1, $33
         55        ASSIGN                                                   !1, ~34
    9    56    >   PRE_INC                                                  !3
         57    >   IS_SMALLER                                               !3, 100000
         58      > JMPNZ                                                    ~37, ->14
   21    59    >   CONCAT                                           ~38     'total+if+%2F+else+time%3A+', !0
         60        FETCH_CONSTANT                                   ~39     'BR'
         61        CONCAT                                           ~40     ~38, ~39
         62        ECHO                                                     ~40
   22    63        CONCAT                                           ~41     'total+case+time%3A+', !1
         64        FETCH_CONSTANT                                   ~42     'BR'
         65        CONCAT                                           ~43     ~41, ~42
         66        ECHO                                                     ~43
   23    67        CONCAT                                           ~44     'total+array+time%3A+', !2
         68        FETCH_CONSTANT                                   ~45     'BR'
         69        CONCAT                                           ~46     ~44, ~45
         70        ECHO                                                     ~46
   24    71        FETCH_CONSTANT                                   ~47     'BR'
         72        ECHO                                                     ~47
   25    73        IS_SMALLER                                               !1, !0
         74      > JMPZ                                                     ~48, ->81
   26    75    >   SUB                                              ~49     !0, !1
         76        CONCAT                                           ~50     'switch+is+quicker+by+', ~49
         77        FETCH_CONSTANT                                   ~51     'BR'
         78        CONCAT                                           ~52     ~50, ~51
         79        ECHO                                                     ~52
   25    80      > JMP                                                      ->86
   28    81    >   SUB                                              ~53     !1, !0
         82        CONCAT                                           ~54     'if+%2F+else+is+quicker+by+', ~53
         83        FETCH_CONSTANT                                   ~55     'BR'
         84        CONCAT                                           ~56     ~54, ~55
         85        ECHO                                                     ~56
   71    86    > > RETURN                                                   1

Function testswitch:
Finding entry points
Branch analysis from position: 0
7 jumps found. (Code = 187) Position 1 = 17, Position 2 = 19, Position 3 = 21, Position 4 = 23, Position 5 = 25, Position 6 = 27, Position 7 = 6
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
Branch analysis from position: 23
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
Branch analysis from position: 27
Branch analysis from position: 6
2 jumps found. (Code = 44) Position 1 = 8, Position 2 = 17
Branch analysis from position: 8
2 jumps found. (Code = 44) Position 1 = 10, Position 2 = 19
Branch analysis from position: 10
2 jumps found. (Code = 44) Position 1 = 12, Position 2 = 21
Branch analysis from position: 12
2 jumps found. (Code = 44) Position 1 = 14, Position 2 = 23
Branch analysis from position: 14
2 jumps found. (Code = 44) Position 1 = 16, Position 2 = 25
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
Branch analysis from position: 25
Branch analysis from position: 23
Branch analysis from position: 21
Branch analysis from position: 19
Branch analysis from position: 17
filename:       /in/t8CM0
function name:  testSwitch
number of ops:  35
compiled vars:  !0 = $oneToFive, !1 = $time_start, !2 = $z, !3 = $time_end, !4 = $time
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   32     0  E >   RECV                                             !0      
   33     1        INIT_FCALL                                               'microtime'
          2        SEND_VAL                                                 <true>
          3        DO_ICALL                                         $5      
          4        ASSIGN                                                   !1, $5
   34     5      > SWITCH_LONG                                              !0, [ 1:->17, 2:->19, 3:->21, 4:->23, 5:->25, ], ->27
   35     6    >   IS_EQUAL                                                 !0, 1
          7      > JMPNZ                                                    ~7, ->17
   36     8    >   IS_EQUAL                                                 !0, 2
          9      > JMPNZ                                                    ~7, ->19
   37    10    >   IS_EQUAL                                                 !0, 3
         11      > JMPNZ                                                    ~7, ->21
   38    12    >   IS_EQUAL                                                 !0, 4
         13      > JMPNZ                                                    ~7, ->23
   39    14    >   IS_EQUAL                                                 !0, 5
         15      > JMPNZ                                                    ~7, ->25
         16    > > JMP                                                      ->27
   35    17    >   ASSIGN                                                   !2, 1
         18      > JMP                                                      ->27
   36    19    >   ASSIGN                                                   !2, 2
         20      > JMP                                                      ->27
   37    21    >   ASSIGN                                                   !2, 3
         22      > JMP                                                      ->27
   38    23    >   ASSIGN                                                   !2, 4
         24      > JMP                                                      ->27
   39    25    >   ASSIGN                                                   !2, 5
         26      > JMP                                                      ->27
   41    27    >   INIT_FCALL                                               'microtime'
         28        SEND_VAL                                                 <true>
         29        DO_ICALL                                         $13     
         30        ASSIGN                                                   !3, $13
   42    31        SUB                                              ~15     !3, !1
         32        ASSIGN                                                   !4, ~15
   43    33      > RETURN                                                   !4
   44    34*     > RETURN                                                   null

End of function testswitch

Function testif:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 9
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 13
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 17
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 21
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
Branch analysis from position: 21
2 jumps found. (Code = 43) Position 1 = 23, Position 2 = 24
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 24
filename:       /in/t8CM0
function name:  testIf
number of ops:  32
compiled vars:  !0 = $oneToFive, !1 = $time_start, !2 = $z, !3 = $time_end, !4 = $time
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   46     0  E >   RECV                                             !0      
   47     1        INIT_FCALL                                               'microtime'
          2        SEND_VAL                                                 <true>
          3        DO_ICALL                                         $5      
          4        ASSIGN                                                   !1, $5
   48     5        IS_EQUAL                                                 !0, 1
          6      > JMPZ                                                     ~7, ->9
   49     7    >   ASSIGN                                                   !2, 1
   48     8      > JMP                                                      ->24
   50     9    >   IS_EQUAL                                                 !0, 2
         10      > JMPZ                                                     ~9, ->13
   51    11    >   ASSIGN                                                   !2, 2
   50    12      > JMP                                                      ->24
   52    13    >   IS_EQUAL                                                 !0, 3
         14      > JMPZ                                                     ~11, ->17
   53    15    >   ASSIGN                                                   !2, 3
   52    16      > JMP                                                      ->24
   54    17    >   IS_EQUAL                                                 !0, 4
         18      > JMPZ                                                     ~13, ->21
   55    19    >   ASSIGN                                                   !2, 4
   54    20      > JMP                                                      ->24
   56    21    >   IS_EQUAL                                                 !0, 5
         22      > JMPZ                                                     ~15, ->24
   57    23    >   ASSIGN                                                   !2, 5
   59    24    >   INIT_FCALL                                               'microtime'
         25        SEND_VAL                                                 <true>
         26        DO_ICALL                                         $17     
         27        ASSIGN                                                   !3, $17
   60    28        SUB                                              ~19     !3, !1
         29        ASSIGN                                                   !4, ~19
   61    30      > RETURN                                                   !4
   62    31*     > RETURN                                                   null

End of function testif

Function testarray:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/t8CM0
function name:  testArray
number of ops:  16
compiled vars:  !0 = $oneToFive, !1 = $time_start, !2 = $array, !3 = $result, !4 = $time_end, !5 = $time
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   64     0  E >   RECV                                             !0      
   65     1        INIT_FCALL                                               'microtime'
          2        SEND_VAL                                                 <true>
          3        DO_ICALL                                         $6      
          4        ASSIGN                                                   !1, $6
   66     5        ASSIGN                                                   !2, <array>
   67     6        FETCH_DIM_R                                      ~9      !2, !0
          7        ASSIGN                                                   !3, ~9
   68     8        INIT_FCALL                                               'microtime'
          9        SEND_VAL                                                 <true>
         10        DO_ICALL                                         $11     
         11        ASSIGN                                                   !4, $11
   69    12        SUB                                              ~13     !4, !1
         13        ASSIGN                                                   !5, ~13
   70    14      > RETURN                                                   !5
   71    15*     > RETURN                                                   null

End of function testarray

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
146.62 ms | 1479 KiB | 17 Q