3v4l.org

run code in 300+ PHP versions simultaneously
<?php function timeClosures( $n ) { $start = microtime( true ); for ( $i = 0; $i < $n; $i++ ) { $closure = function( $x ) use ( $i ) { return $i*$x; }; } $sec = microtime( true ) - $start; print " It took $sec seconds to create $n closures.\n"; return $sec; } class ClosureBenchmarkTestClass { private $x; public function __construct( $x ) { $this->x = $x; } public function foo( $y ) { return $this->x * $y; } } function timeObjects( $n ) { $start = microtime( true ); for ( $i = 0; $i < $n; $i++ ) { $obj = new ClosureBenchmarkTestClass( $i ); } $sec = microtime( true ) - $start; print " It took $sec seconds to create $n objects.\n"; return $sec; } $m = 10; $n = 1000000; for ( $i = 0; $i < $m; $i++ ) { $ctime = timeClosures( $n ); $otime = timeObjects( $n ); $dtime = $ctime - $otime; $rtime = ( $ctime / $otime ); $fasterOrSlower = $dtime > 0 ? 'faster' : 'slower'; print sprintf( "Creating %d objects was %f seconds %s (%d%%).\n", $n, abs( $dtime ), $fasterOrSlower, abs( $rtime ) * 100 ); }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 38
Branch analysis from position: 38
2 jumps found. (Code = 44) Position 1 = 40, Position 2 = 4
Branch analysis from position: 40
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 20
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 21
Branch analysis from position: 21
2 jumps found. (Code = 44) Position 1 = 40, Position 2 = 4
Branch analysis from position: 40
Branch analysis from position: 4
Branch analysis from position: 20
2 jumps found. (Code = 44) Position 1 = 40, Position 2 = 4
Branch analysis from position: 40
Branch analysis from position: 4
filename:       /in/barGj
function name:  (null)
number of ops:  41
compiled vars:  !0 = $m, !1 = $n, !2 = $i, !3 = $ctime, !4 = $otime, !5 = $dtime, !6 = $rtime, !7 = $fasterOrSlower
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   41     0  E >   ASSIGN                                                   !0, 10
   42     1        ASSIGN                                                   !1, 1000000
   44     2        ASSIGN                                                   !2, 0
          3      > JMP                                                      ->38
   45     4    >   INIT_FCALL                                               'timeclosures'
          5        SEND_VAR                                                 !1
          6        DO_FCALL                                      0  $11     
          7        ASSIGN                                                   !3, $11
   46     8        INIT_FCALL                                               'timeobjects'
          9        SEND_VAR                                                 !1
         10        DO_FCALL                                      0  $13     
         11        ASSIGN                                                   !4, $13
   48    12        SUB                                              ~15     !3, !4
         13        ASSIGN                                                   !5, ~15
   49    14        DIV                                              ~17     !3, !4
         15        ASSIGN                                                   !6, ~17
   50    16        IS_SMALLER                                               0, !5
         17      > JMPZ                                                     ~19, ->20
         18    >   QM_ASSIGN                                        ~20     'faster'
         19      > JMP                                                      ->21
         20    >   QM_ASSIGN                                        ~20     'slower'
         21    >   ASSIGN                                                   !7, ~20
   51    22        INIT_FCALL                                               'sprintf'
         23        SEND_VAL                                                 'Creating+%25d+objects+was+%25f+seconds+%25s+%28%25d%25%25%29.%0A'
         24        SEND_VAR                                                 !1
         25        INIT_FCALL                                               'abs'
   52    26        SEND_VAR                                                 !5
         27        DO_ICALL                                         $22     
         28        SEND_VAR                                                 $22
         29        SEND_VAR                                                 !7
         30        INIT_FCALL                                               'abs'
         31        SEND_VAR                                                 !6
         32        DO_ICALL                                         $23     
         33        MUL                                              ~24     $23, 100
         34        SEND_VAL                                                 ~24
         35        DO_ICALL                                         $25     
         36        ECHO                                                     $25
   44    37        PRE_INC                                                  !2
         38    >   IS_SMALLER                                               !2, !0
         39      > JMPNZ                                                    ~27, ->4
   53    40    > > RETURN                                                   1

Function timeclosures:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
2 jumps found. (Code = 44) Position 1 = 13, Position 2 = 7
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
2 jumps found. (Code = 44) Position 1 = 13, Position 2 = 7
Branch analysis from position: 13
Branch analysis from position: 7
filename:       /in/barGj
function name:  timeClosures
number of ops:  26
compiled vars:  !0 = $n, !1 = $start, !2 = $i, !3 = $closure, !4 = $sec
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    4     1        INIT_FCALL                                               'microtime'
          2        SEND_VAL                                                 <true>
          3        DO_ICALL                                         $5      
          4        ASSIGN                                                   !1, $5
    6     5        ASSIGN                                                   !2, 0
          6      > JMP                                                      ->11
    7     7    >   DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FbarGj%3A7%240'
          8        BIND_LEXICAL                                             ~8, !2
          9        ASSIGN                                                   !3, ~8
    6    10        PRE_INC                                                  !2
         11    >   IS_SMALLER                                               !2, !0
         12      > JMPNZ                                                    ~11, ->7
   10    13    >   INIT_FCALL                                               'microtime'
         14        SEND_VAL                                                 <true>
         15        DO_ICALL                                         $12     
         16        SUB                                              ~13     $12, !1
         17        ASSIGN                                                   !4, ~13
   11    18        ROPE_INIT                                     5  ~16     '++It+took+'
         19        ROPE_ADD                                      1  ~16     ~16, !4
         20        ROPE_ADD                                      2  ~16     ~16, '+seconds+to+create+'
         21        ROPE_ADD                                      3  ~16     ~16, !0
         22        ROPE_END                                      4  ~15     ~16, '+closures.%0A'
         23        ECHO                                                     ~15
   13    24      > RETURN                                                   !4
   14    25*     > RETURN                                                   null

End of function timeclosures

Function %00%7Bclosure%7D%2Fin%2FbarGj%3A7%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/barGj
function name:  {closure}
number of ops:  5
compiled vars:  !0 = $x, !1 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
          2        MUL                                              ~2      !1, !0
          3      > RETURN                                                   ~2
          4*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FbarGj%3A7%240

Function timeobjects:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
2 jumps found. (Code = 44) Position 1 = 14, Position 2 = 7
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
2 jumps found. (Code = 44) Position 1 = 14, Position 2 = 7
Branch analysis from position: 14
Branch analysis from position: 7
filename:       /in/barGj
function name:  timeObjects
number of ops:  27
compiled vars:  !0 = $n, !1 = $start, !2 = $i, !3 = $obj, !4 = $sec
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   28     0  E >   RECV                                             !0      
   29     1        INIT_FCALL                                               'microtime'
          2        SEND_VAL                                                 <true>
          3        DO_ICALL                                         $5      
          4        ASSIGN                                                   !1, $5
   31     5        ASSIGN                                                   !2, 0
          6      > JMP                                                      ->12
   32     7    >   NEW                                              $8      'ClosureBenchmarkTestClass'
          8        SEND_VAR_EX                                              !2
          9        DO_FCALL                                      0          
         10        ASSIGN                                                   !3, $8
   31    11        PRE_INC                                                  !2
         12    >   IS_SMALLER                                               !2, !0
         13      > JMPNZ                                                    ~12, ->7
   35    14    >   INIT_FCALL                                               'microtime'
         15        SEND_VAL                                                 <true>
         16        DO_ICALL                                         $13     
         17        SUB                                              ~14     $13, !1
         18        ASSIGN                                                   !4, ~14
   36    19        ROPE_INIT                                     5  ~17     '++It+took+'
         20        ROPE_ADD                                      1  ~17     ~17, !4
         21        ROPE_ADD                                      2  ~17     ~17, '+seconds+to+create+'
         22        ROPE_ADD                                      3  ~17     ~17, !0
         23        ROPE_END                                      4  ~16     ~17, '+objects.%0A'
         24        ECHO                                                     ~16
   38    25      > RETURN                                                   !4
   39    26*     > RETURN                                                   null

End of function timeobjects

Class ClosureBenchmarkTestClass:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/barGj
function name:  __construct
number of ops:  4
compiled vars:  !0 = $x
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   RECV                                             !0      
   20     1        ASSIGN_OBJ                                               'x'
          2        OP_DATA                                                  !0
   21     3      > RETURN                                                   null

End of function __construct

Function foo:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/barGj
function name:  foo
number of ops:  5
compiled vars:  !0 = $y
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   23     0  E >   RECV                                             !0      
   24     1        FETCH_OBJ_R                                      ~1      'x'
          2        MUL                                              ~2      !0, ~1
          3      > RETURN                                                   ~2
   25     4*     > RETURN                                                   null

End of function foo

End of class ClosureBenchmarkTestClass.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
156.34 ms | 1411 KiB | 21 Q