3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** strrev function implementation taken from ext/standard/string.c PHP-5-5 branch http://lxr.php.net/xref/PHP_5_5/ext/standard/string.c#3165 // {{{ proto string strrev(string str) // Reverse a string PHP_FUNCTION(strrev) { char *str; char *e, *n, *p; int str_len; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) { return; } n = emalloc(str_len+1); p = n; e = str + str_len; while (--e>=str) { *p++ = *e; } *p = '\0'; RETVAL_STRINGL(n, str_len, 0); } // }}} */ // PHP Implementation function userland_strrev($string) { if (!is_string($string)) { return false; } $new_string = ""; $len = strlen($string); while (--$len > -1) { $new_string .= $string[$len]; } return $new_string; } $string = "Hello PHP!"; $c = 1000; $results = array(); $tests = array('strrev', 'userland_strrev'); foreach ($tests as $test) { for ($i = 0; $i < $c; $i++) { $t = microtime(true); $test($string); $results[$test][] = microtime(true) - $t; } $avg = array_sum($results[$test]) / count($results[$test]); printf("Average cost for function $test: %.6f ms\n", $avg); } // Without callable string resolution for ($i = 0; $i < $c; $i++) { $t = microtime(true); strrev($string); $results[$test][] = microtime(true) - $t; } $avg = array_sum($results[$test]) / count($results[$test]); printf("Average cost for function strrev: %.6f ms\n", $avg); for ($i = 0; $i < $c; $i++) { $t = microtime(true); userland_strrev($string); $results[$test][] = microtime(true) - $t; } $avg = array_sum($results[$test]) / count($results[$test]); printf("Average cost for function userland_strrev: %.6f ms\n", $avg);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 41
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 41
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 23
Branch analysis from position: 23
2 jumps found. (Code = 44) Position 1 = 25, Position 2 = 8
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 8
2 jumps found. (Code = 44) Position 1 = 25, Position 2 = 8
Branch analysis from position: 25
Branch analysis from position: 8
Branch analysis from position: 41
1 jumps found. (Code = 42) Position 1 = 59
Branch analysis from position: 59
2 jumps found. (Code = 44) Position 1 = 61, Position 2 = 44
Branch analysis from position: 61
1 jumps found. (Code = 42) Position 1 = 90
Branch analysis from position: 90
2 jumps found. (Code = 44) Position 1 = 92, Position 2 = 75
Branch analysis from position: 92
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 75
2 jumps found. (Code = 44) Position 1 = 92, Position 2 = 75
Branch analysis from position: 92
Branch analysis from position: 75
Branch analysis from position: 44
2 jumps found. (Code = 44) Position 1 = 61, Position 2 = 44
Branch analysis from position: 61
Branch analysis from position: 44
Branch analysis from position: 41
filename:       /in/u0BM0
function name:  (null)
number of ops:  105
compiled vars:  !0 = $string, !1 = $c, !2 = $results, !3 = $tests, !4 = $test, !5 = $i, !6 = $t, !7 = $avg
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   50     0  E >   ASSIGN                                                   !0, 'Hello+PHP%21'
   51     1        ASSIGN                                                   !1, 1000
   52     2        ASSIGN                                                   !2, <array>
   53     3        ASSIGN                                                   !3, <array>
   55     4      > FE_RESET_R                                       $12     !3, ->41
          5    > > FE_FETCH_R                                               $12, !4, ->41
   57     6    >   ASSIGN                                                   !5, 0
          7      > JMP                                                      ->23
   58     8    >   INIT_FCALL                                               'microtime'
          9        SEND_VAL                                                 <true>
         10        DO_ICALL                                         $14     
         11        ASSIGN                                                   !6, $14
   59    12        INIT_DYNAMIC_CALL                                        !4
         13        SEND_VAR_EX                                              !0
         14        DO_FCALL                                      0          
   60    15        INIT_FCALL                                               'microtime'
         16        SEND_VAL                                                 <true>
         17        DO_ICALL                                         $19     
         18        SUB                                              ~20     $19, !6
         19        FETCH_DIM_W                                      $17     !2, !4
         20        ASSIGN_DIM                                               $17
         21        OP_DATA                                                  ~20
   57    22        PRE_INC                                                  !5
         23    >   IS_SMALLER                                               !5, !1
         24      > JMPNZ                                                    ~22, ->8
   62    25    >   INIT_FCALL                                               'array_sum'
         26        FETCH_DIM_R                                      ~23     !2, !4
         27        SEND_VAL                                                 ~23
         28        DO_ICALL                                         $24     
         29        FETCH_DIM_R                                      ~25     !2, !4
         30        COUNT                                            ~26     ~25
         31        DIV                                              ~27     $24, ~26
         32        ASSIGN                                                   !7, ~27
   63    33        INIT_FCALL                                               'printf'
         34        ROPE_INIT                                     3  ~30     'Average+cost+for+function+'
         35        ROPE_ADD                                      1  ~30     ~30, !4
         36        ROPE_END                                      2  ~29     ~30, '%3A+%25.6f+ms%0A'
         37        SEND_VAL                                                 ~29
         38        SEND_VAR                                                 !7
         39        DO_ICALL                                                 
   55    40      > JMP                                                      ->5
         41    >   FE_FREE                                                  $12
   68    42        ASSIGN                                                   !5, 0
         43      > JMP                                                      ->59
   69    44    >   INIT_FCALL                                               'microtime'
         45        SEND_VAL                                                 <true>
         46        DO_ICALL                                         $34     
         47        ASSIGN                                                   !6, $34
   70    48        INIT_FCALL                                               'strrev'
         49        SEND_VAR                                                 !0
         50        DO_ICALL                                                 
   71    51        INIT_FCALL                                               'microtime'
         52        SEND_VAL                                                 <true>
         53        DO_ICALL                                         $39     
         54        SUB                                              ~40     $39, !6
         55        FETCH_DIM_W                                      $37     !2, !4
         56        ASSIGN_DIM                                               $37
         57        OP_DATA                                                  ~40
   68    58        PRE_INC                                                  !5
         59    >   IS_SMALLER                                               !5, !1
         60      > JMPNZ                                                    ~42, ->44
   73    61    >   INIT_FCALL                                               'array_sum'
         62        FETCH_DIM_R                                      ~43     !2, !4
         63        SEND_VAL                                                 ~43
         64        DO_ICALL                                         $44     
         65        FETCH_DIM_R                                      ~45     !2, !4
         66        COUNT                                            ~46     ~45
         67        DIV                                              ~47     $44, ~46
         68        ASSIGN                                                   !7, ~47
   74    69        INIT_FCALL                                               'printf'
         70        SEND_VAL                                                 'Average+cost+for+function+strrev%3A+%25.6f+ms%0A'
         71        SEND_VAR                                                 !7
         72        DO_ICALL                                                 
   76    73        ASSIGN                                                   !5, 0
         74      > JMP                                                      ->90
   77    75    >   INIT_FCALL                                               'microtime'
         76        SEND_VAL                                                 <true>
         77        DO_ICALL                                         $51     
         78        ASSIGN                                                   !6, $51
   78    79        INIT_FCALL                                               'userland_strrev'
         80        SEND_VAR                                                 !0
         81        DO_FCALL                                      0          
   79    82        INIT_FCALL                                               'microtime'
         83        SEND_VAL                                                 <true>
         84        DO_ICALL                                         $56     
         85        SUB                                              ~57     $56, !6
         86        FETCH_DIM_W                                      $54     !2, !4
         87        ASSIGN_DIM                                               $54
         88        OP_DATA                                                  ~57
   76    89        PRE_INC                                                  !5
         90    >   IS_SMALLER                                               !5, !1
         91      > JMPNZ                                                    ~59, ->75
   81    92    >   INIT_FCALL                                               'array_sum'
         93        FETCH_DIM_R                                      ~60     !2, !4
         94        SEND_VAL                                                 ~60
         95        DO_ICALL                                         $61     
         96        FETCH_DIM_R                                      ~62     !2, !4
         97        COUNT                                            ~63     ~62
         98        DIV                                              ~64     $61, ~63
         99        ASSIGN                                                   !7, ~64
   82   100        INIT_FCALL                                               'printf'
        101        SEND_VAL                                                 'Average+cost+for+function+userland_strrev%3A+%25.6f+ms%0A'
        102        SEND_VAR                                                 !7
        103        DO_ICALL                                                 
        104      > RETURN                                                   1

Function userland_strrev:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 5
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
2 jumps found. (Code = 44) Position 1 = 14, Position 2 = 9
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
2 jumps found. (Code = 44) Position 1 = 14, Position 2 = 9
Branch analysis from position: 14
Branch analysis from position: 9
filename:       /in/u0BM0
function name:  userland_strrev
number of ops:  16
compiled vars:  !0 = $string, !1 = $new_string, !2 = $len
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     0  E >   RECV                                             !0      
   36     1        TYPE_CHECK                                   64  ~3      !0
          2        BOOL_NOT                                         ~4      ~3
          3      > JMPZ                                                     ~4, ->5
   37     4    > > RETURN                                                   <false>
   40     5    >   ASSIGN                                                   !1, ''
   41     6        STRLEN                                           ~6      !0
          7        ASSIGN                                                   !2, ~6
   43     8      > JMP                                                      ->11
   44     9    >   FETCH_DIM_R                                      ~8      !0, !2
         10        ASSIGN_OP                                     8          !1, ~8
   43    11    >   PRE_DEC                                          ~10     !2
         12        IS_SMALLER                                               -1, ~10
         13      > JMPNZ                                                    ~11, ->9
   47    14    > > RETURN                                                   !1
   48    15*     > RETURN                                                   null

End of function userland_strrev

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
155.88 ms | 1411 KiB | 22 Q