3v4l.org

run code in 500+ PHP versions simultaneously
<?php namespace Bench; /** * 1. ZEND_IS_BOOL 命令にコンパイルされる (最速期待) */ function is_bool_native(mixed $v): bool { return \is_bool($v); } /** * 2. 名前空間解決による DO_FCALL。実行時にグローバル関数を探す */ function is_bool_ns(mixed $v): bool { return is_bool($v); } /** * 3. ZEND_IN_ARRAY 命令にコンパイルされる (O(1) 最適化) */ function in_array_native(mixed $v): bool { return \in_array($v, [true, false], true); } /** * 4. 名前空間解決 + DO_FCALL + 通常の線形探索 (O(n)) */ function in_array_ns(mixed $v): bool { return in_array($v, [true, false], true); } /** * 5. ユーザー定義ロジック:キャストと比較 */ function is_bool_cast(mixed $v): bool { return $v === (bool)$v; } /** * 6. ユーザー定義ロジック:論理和 */ function is_bool_or(mixed $v): bool { return $v === true || $v === false; } /** * 7. match式 */ function is_bool_match(mixed $v): bool { return match ($v) { true, false => true, default => false, }; } /** * データセット */ $datasets = [ 'all_bool' => [true, false, true, false], 'no_bool' => [1, 0, "1", "0", "true", [], new \stdClass(), null], 'half_half' => [true, 1, false, "test", true, null, false, []], ]; $testcases = [ 'is_bool (\native)' => is_bool_native(...), 'is_bool (ns)' => is_bool_ns(...), 'in_array (\native)' => in_array_native(...), 'in_array (ns)' => in_array_ns(...), 'is_bool_cast' => is_bool_cast(...), 'logical OR' => is_bool_or(...), 'match_expression' => is_bool_match(...), ]; $iterations = 100000; echo "PHP Version: " . PHP_VERSION . "\n"; echo str_repeat('=', 60) . "\n"; foreach ($datasets as $data_name => $values) { echo "\nDataset: $data_name\n"; echo str_repeat('-', 60) . "\n"; $results = []; foreach ($testcases as $name => $test) { $start = hrtime(true); for ($i = 0; $i < $iterations; $i++) { foreach ($values as $value) { $test($value); } } $end = hrtime(true); // 合計実行回数で割って平均を出すのではなく、あえて「このデータセット一式の処理時間」とする $results[$name] = ($end - $start) / 1e+6; } asort($results); foreach ($results as $name => $ms) { printf("%-20s : %10.4f ms\n", $name, $ms); } }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 48, Position 2 = 104
Branch analysis from position: 48
2 jumps found. (Code = 78) Position 1 = 49, Position 2 = 104
Branch analysis from position: 49
2 jumps found. (Code = 77) Position 1 = 62, Position 2 = 89
Branch analysis from position: 62
2 jumps found. (Code = 78) Position 1 = 63, Position 2 = 89
Branch analysis from position: 63
1 jumps found. (Code = 42) Position 1 = 78
Branch analysis from position: 78
2 jumps found. (Code = 44) Position 1 = 80, Position 2 = 70
Branch analysis from position: 80
1 jumps found. (Code = 42) Position 1 = 62
Branch analysis from position: 62
Branch analysis from position: 70
2 jumps found. (Code = 77) Position 1 = 71, Position 2 = 76
Branch analysis from position: 71
2 jumps found. (Code = 78) Position 1 = 72, Position 2 = 76
Branch analysis from position: 72
1 jumps found. (Code = 42) Position 1 = 71
Branch analysis from position: 71
Branch analysis from position: 76
2 jumps found. (Code = 44) Position 1 = 80, Position 2 = 70
Branch analysis from position: 80
Branch analysis from position: 70
Branch analysis from position: 76
Branch analysis from position: 89
2 jumps found. (Code = 77) Position 1 = 94, Position 2 = 102
Branch analysis from position: 94
2 jumps found. (Code = 78) Position 1 = 95, Position 2 = 102
Branch analysis from position: 95
1 jumps found. (Code = 42) Position 1 = 94
Branch analysis from position: 94
Branch analysis from position: 102
1 jumps found. (Code = 42) Position 1 = 48
Branch analysis from position: 48
Branch analysis from position: 102
Branch analysis from position: 89
Branch analysis from position: 104
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 104
filename:       /in/oa3V2
function name:  (null)
number of ops:  106
compiled vars:  !0 = $datasets, !1 = $testcases, !2 = $iterations, !3 = $values, !4 = $data_name, !5 = $results, !6 = $test, !7 = $name, !8 = $start, !9 = $i, !10 = $value, !11 = $end, !12 = $ms
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   50     0  E >   INIT_ARRAY                                           ~13     <array>, 'all_bool'
   51     1        INIT_ARRAY                                           ~14     1
          2        ADD_ARRAY_ELEMENT                                    ~14     0
          3        ADD_ARRAY_ELEMENT                                    ~14     '1'
          4        ADD_ARRAY_ELEMENT                                    ~14     '0'
          5        ADD_ARRAY_ELEMENT                                    ~14     'true'
   50     6        ADD_ARRAY_ELEMENT                                    ~14     <array>
   51     7        NEW                                                  $15     'stdClass'
          8        DO_FCALL                                          0          
          9        ADD_ARRAY_ELEMENT                                    ~14     $15
   50    10        ADD_ARRAY_ELEMENT                                    ~14     null
         11        ADD_ARRAY_ELEMENT                                    ~13     ~14, 'no_bool'
         12        ADD_ARRAY_ELEMENT                                    ~13     <array>, 'half_half'
   49    13        ASSIGN                                                       !0, ~13
   56    14        INIT_NS_FCALL_BY_NAME                                        'Bench%5Cis_bool_native'
         15        CALLABLE_CONVERT                                     ~18     
         16        INIT_ARRAY                                           ~19     ~18, 'is_bool+%28%5Cnative%29'
   57    17        INIT_NS_FCALL_BY_NAME                                        'Bench%5Cis_bool_ns'
         18        CALLABLE_CONVERT                                     ~20     
         19        ADD_ARRAY_ELEMENT                                    ~19     ~20, 'is_bool+%28ns%29'
   58    20        INIT_NS_FCALL_BY_NAME                                        'Bench%5Cin_array_native'
         21        CALLABLE_CONVERT                                     ~21     
         22        ADD_ARRAY_ELEMENT                                    ~19     ~21, 'in_array+%28%5Cnative%29'
   59    23        INIT_NS_FCALL_BY_NAME                                        'Bench%5Cin_array_ns'
         24        CALLABLE_CONVERT                                     ~22     
         25        ADD_ARRAY_ELEMENT                                    ~19     ~22, 'in_array+%28ns%29'
   60    26        INIT_NS_FCALL_BY_NAME                                        'Bench%5Cis_bool_cast'
         27        CALLABLE_CONVERT                                     ~23     
         28        ADD_ARRAY_ELEMENT                                    ~19     ~23, 'is_bool_cast'
   61    29        INIT_NS_FCALL_BY_NAME                                        'Bench%5Cis_bool_or'
         30        CALLABLE_CONVERT                                     ~24     
         31        ADD_ARRAY_ELEMENT                                    ~19     ~24, 'logical+OR'
   62    32        INIT_NS_FCALL_BY_NAME                                        'Bench%5Cis_bool_match'
         33        CALLABLE_CONVERT                                     ~25     
         34        ADD_ARRAY_ELEMENT                                    ~19     ~25, 'match_expression'
   55    35        ASSIGN                                                       !1, ~19
   65    36        ASSIGN                                                       !2, 100000
   67    37        FETCH_CONSTANT                                       ~28     'Bench%5CPHP_VERSION'
         38        CONCAT                                               ~29     'PHP+Version%3A+', ~28
         39        CONCAT                                               ~30     ~29, '%0A'
         40        ECHO                                                         ~30
   68    41        INIT_NS_FCALL_BY_NAME                                        'Bench%5Cstr_repeat'
         42        SEND_VAL_EX                                                  '%3D'
         43        SEND_VAL_EX                                                  60
         44        DO_FCALL                                          0  $31     
         45        CONCAT                                               ~32     $31, '%0A'
         46        ECHO                                                         ~32
   70    47      > FE_RESET_R                                           $33     !0, ->104
         48    > > FE_FETCH_R                                           ~34     $33, !3, ->104
         49    >   ASSIGN                                                       !4, ~34
   71    50        ROPE_INIT                                         3  ~37     '%0ADataset%3A+'
         51        ROPE_ADD                                          1  ~37     ~37, !4
         52        ROPE_END                                          2  ~36     ~37, '%0A'
         53        ECHO                                                         ~36
   72    54        INIT_NS_FCALL_BY_NAME                                        'Bench%5Cstr_repeat'
         55        SEND_VAL_EX                                                  '-'
         56        SEND_VAL_EX                                                  60
         57        DO_FCALL                                          0  $39     
         58        CONCAT                                               ~40     $39, '%0A'
         59        ECHO                                                         ~40
   74    60        ASSIGN                                                       !5, <array>
   75    61      > FE_RESET_R                                           $42     !1, ->89
         62    > > FE_FETCH_R                                           ~43     $42, !6, ->89
         63    >   ASSIGN                                                       !7, ~43
   76    64        INIT_NS_FCALL_BY_NAME                                        'Bench%5Chrtime'
         65        SEND_VAL_EX                                                  <true>
         66        DO_FCALL                                          0  $45     
         67        ASSIGN                                                       !8, $45
   77    68        ASSIGN                                                       !9, 0
         69      > JMP                                                          ->78
   78    70    > > FE_RESET_R                                           $48     !3, ->76
         71    > > FE_FETCH_R                                                   $48, !10, ->76
   79    72    >   INIT_DYNAMIC_CALL                                            !6
         73        SEND_VAR_EX                                                  !10
         74        DO_FCALL                                          0          
   78    75      > JMP                                                          ->71
         76    >   FE_FREE                                                      $48
   77    77        PRE_INC                                                      !9
         78    >   IS_SMALLER                                                   !9, !2
         79      > JMPNZ                                                        ~51, ->70
   82    80    >   INIT_NS_FCALL_BY_NAME                                        'Bench%5Chrtime'
         81        SEND_VAL_EX                                                  <true>
         82        DO_FCALL                                          0  $52     
         83        ASSIGN                                                       !11, $52
   84    84        SUB                                                  ~55     !11, !8
         85        DIV                                                  ~56     ~55, 1.0e+6
         86        ASSIGN_DIM                                                   !5, !7
         87        OP_DATA                                                      ~56
   75    88      > JMP                                                          ->62
         89    >   FE_FREE                                                      $42
   87    90        INIT_NS_FCALL_BY_NAME                                        'Bench%5Casort'
         91        SEND_VAR_EX                                                  !5
         92        DO_FCALL                                          0          
   88    93      > FE_RESET_R                                           $58     !5, ->102
         94    > > FE_FETCH_R                                           ~59     $58, !12, ->102
         95    >   ASSIGN                                                       !7, ~59
   89    96        INIT_NS_FCALL_BY_NAME                                        'Bench%5Cprintf'
         97        SEND_VAL_EX                                                  '%25-20s+%3A+%2510.4f+ms%0A'
         98        SEND_VAR_EX                                                  !7
         99        SEND_VAR_EX                                                  !12
        100        DO_FCALL                                          0          
   88   101      > JMP                                                          ->94
        102    >   FE_FREE                                                      $58
   70   103      > JMP                                                          ->48
        104    >   FE_FREE                                                      $33
   91   105      > RETURN                                                       1

Function bench%5Cis_bool_native:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/oa3V2
function name:  Bench\is_bool_native
number of ops:  6
compiled vars:  !0 = $v
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    8     0  E >   RECV                                                 !0      
          1        TYPE_CHECK                                       12  ~1      !0
          2        VERIFY_RETURN_TYPE                                           ~1
          3      > RETURN                                                       ~1
          4*       VERIFY_RETURN_TYPE                                           
          5*     > RETURN                                                       null

End of function bench%5Cis_bool_native

Function bench%5Cis_bool_ns:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/oa3V2
function name:  Bench\is_bool_ns
number of ops:  8
compiled vars:  !0 = $v
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   13     0  E >   RECV                                                 !0      
          1        INIT_NS_FCALL_BY_NAME                                        'Bench%5Cis_bool'
          2        SEND_VAR_EX                                                  !0
          3        DO_FCALL                                          0  $1      
          4        VERIFY_RETURN_TYPE                                           $1
          5      > RETURN                                                       $1
          6*       VERIFY_RETURN_TYPE                                           
          7*     > RETURN                                                       null

End of function bench%5Cis_bool_ns

Function bench%5Cin_array_native:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/oa3V2
function name:  Bench\in_array_native
number of ops:  7
compiled vars:  !0 = $v
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   18     0  E >   RECV                                                 !0      
          1        FRAMELESS_ICALL_3                in_array            ~1      !0, <array>
          2        OP_DATA                                                      <true>
          3        VERIFY_RETURN_TYPE                                           ~1
          4      > RETURN                                                       ~1
          5*       VERIFY_RETURN_TYPE                                           
          6*     > RETURN                                                       null

End of function bench%5Cin_array_native

Function bench%5Cin_array_ns:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 208) Position 1 = 2, Position 2 = 8
Branch analysis from position: 2
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/oa3V2
function name:  Bench\in_array_ns
number of ops:  14
compiled vars:  !0 = $v
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   23     0  E >   RECV                                                 !0      
          1      > JMP_FRAMELESS                                   s8           'bench%5Cin_array', ->8
          2    >   INIT_NS_FCALL_BY_NAME                                        'Bench%5Cin_array'
          3        SEND_VAR_EX                                                  !0
          4        SEND_VAL_EX                                                  <array>
          5        SEND_VAL_EX                                                  <true>
          6        DO_FCALL                                          0  $1      
          7      > JMP                                                          ->10
          8    >   FRAMELESS_ICALL_3                in_array            $1      !0, <array>
          9        OP_DATA                                                      <true>
         10    >   VERIFY_RETURN_TYPE                                           $1
         11      > RETURN                                                       $1
         12*       VERIFY_RETURN_TYPE                                           
         13*     > RETURN                                                       null

End of function bench%5Cin_array_ns

Function bench%5Cis_bool_cast:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/oa3V2
function name:  Bench\is_bool_cast
number of ops:  7
compiled vars:  !0 = $v
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   28     0  E >   RECV                                                 !0      
          1        BOOL                                                 ~1      !0
          2        IS_IDENTICAL                                         ~2      !0, ~1
          3        VERIFY_RETURN_TYPE                                           ~2
          4      > RETURN                                                       ~2
          5*       VERIFY_RETURN_TYPE                                           
          6*     > RETURN                                                       null

End of function bench%5Cis_bool_cast

Function bench%5Cis_bool_or:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 47) Position 1 = 3, Position 2 = 5
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
filename:       /in/oa3V2
function name:  Bench\is_bool_or
number of ops:  9
compiled vars:  !0 = $v
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   33     0  E >   RECV                                                 !0      
          1        TYPE_CHECK                                        8  ~1      !0
          2      > JMPNZ_EX                                             ~1      ~1, ->5
          3    >   TYPE_CHECK                                        4  ~2      !0
          4        BOOL                                                 ~1      ~2
          5    >   VERIFY_RETURN_TYPE                                           ~1
          6      > RETURN                                                       ~1
          7*       VERIFY_RETURN_TYPE                                           
          8*     > RETURN                                                       null

End of function bench%5Cis_bool_or

Function bench%5Cis_bool_match:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 44) Position 1 = 3, Position 2 = 6
Branch analysis from position: 3
2 jumps found. (Code = 44) Position 1 = 5, Position 2 = 6
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
Branch analysis from position: 6
filename:       /in/oa3V2
function name:  Bench\is_bool_match
number of ops:  14
compiled vars:  !0 = $v
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   39     0  E >   RECV                                                 !0      
   40     1        IS_IDENTICAL                                                 !0, <true>
          2      > JMPNZ                                                        ~1, ->6
   41     3    >   IS_IDENTICAL                                                 !0, <false>
          4      > JMPNZ                                                        ~1, ->6
          5    > > JMP                                                          ->8
          6    >   QM_ASSIGN                                            ~2      <true>
          7      > JMP                                                          ->10
   42     8    >   QM_ASSIGN                                            ~2      <false>
          9      > JMP                                                          ->10
         10    >   VERIFY_RETURN_TYPE                                           ~2
         11      > RETURN                                                       ~2
   44    12*       VERIFY_RETURN_TYPE                                           
         13*     > RETURN                                                       null

End of function bench%5Cis_bool_match

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
160.41 ms | 1431 KiB | 25 Q