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; } // Tests $string = "Hello PHP!"; $c = 10000; // Without callable string resolution $results = array(); for ($i = 0; $i < $c; $i++) { $t = microtime(true); strrev($string); $results[$test][] = microtime(true) - $t; } array_shift($results[$test]); array_pop($results[$test]); $avg = array_sum($results[$test]) / count($results[$test]); printf("Average cost for function strrev: %.9f s\n", $avg); $results = array(); for ($i = 0; $i < $c; $i++) { $t = microtime(true); userland_strrev($string); $results[$test][] = microtime(true) - $t; } array_shift($results[$test]); array_pop($results[$test]); $avg = array_sum($results[$test]) / count($results[$test]); printf("Average cost for function userland_strrev: %.9f s\n", $avg);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 20
Branch analysis from position: 20
2 jumps found. (Code = 44) Position 1 = 22, Position 2 = 5
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 60
Branch analysis from position: 60
2 jumps found. (Code = 44) Position 1 = 62, Position 2 = 45
Branch analysis from position: 62
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 45
2 jumps found. (Code = 44) Position 1 = 62, Position 2 = 45
Branch analysis from position: 62
Branch analysis from position: 45
Branch analysis from position: 5
2 jumps found. (Code = 44) Position 1 = 22, Position 2 = 5
Branch analysis from position: 22
Branch analysis from position: 5
filename:       /in/DK96q
function name:  (null)
number of ops:  83
compiled vars:  !0 = $string, !1 = $c, !2 = $results, !3 = $i, !4 = $t, !5 = $test, !6 = $avg
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   52     0  E >   ASSIGN                                                   !0, 'Hello+PHP%21'
   53     1        ASSIGN                                                   !1, 10000
   56     2        ASSIGN                                                   !2, <array>
   57     3        ASSIGN                                                   !3, 0
          4      > JMP                                                      ->20
   58     5    >   INIT_FCALL                                               'microtime'
          6        SEND_VAL                                                 <true>
          7        DO_ICALL                                         $11     
          8        ASSIGN                                                   !4, $11
   59     9        INIT_FCALL                                               'strrev'
         10        SEND_VAR                                                 !0
         11        DO_ICALL                                                 
   60    12        INIT_FCALL                                               'microtime'
         13        SEND_VAL                                                 <true>
         14        DO_ICALL                                         $16     
         15        SUB                                              ~17     $16, !4
         16        FETCH_DIM_W                                      $14     !2, !5
         17        ASSIGN_DIM                                               $14
         18        OP_DATA                                                  ~17
   57    19        PRE_INC                                                  !3
         20    >   IS_SMALLER                                               !3, !1
         21      > JMPNZ                                                    ~19, ->5
   62    22    >   INIT_FCALL                                               'array_shift'
         23        FETCH_DIM_W                                      $20     !2, !5
         24        SEND_REF                                                 $20
         25        DO_ICALL                                                 
   63    26        INIT_FCALL                                               'array_pop'
         27        FETCH_DIM_W                                      $22     !2, !5
         28        SEND_REF                                                 $22
         29        DO_ICALL                                                 
   64    30        INIT_FCALL                                               'array_sum'
         31        FETCH_DIM_R                                      ~24     !2, !5
         32        SEND_VAL                                                 ~24
         33        DO_ICALL                                         $25     
         34        FETCH_DIM_R                                      ~26     !2, !5
         35        COUNT                                            ~27     ~26
         36        DIV                                              ~28     $25, ~27
         37        ASSIGN                                                   !6, ~28
   65    38        INIT_FCALL                                               'printf'
         39        SEND_VAL                                                 'Average+cost+for+function+strrev%3A+%25.9f+s%0A'
         40        SEND_VAR                                                 !6
         41        DO_ICALL                                                 
   67    42        ASSIGN                                                   !2, <array>
   68    43        ASSIGN                                                   !3, 0
         44      > JMP                                                      ->60
   69    45    >   INIT_FCALL                                               'microtime'
         46        SEND_VAL                                                 <true>
         47        DO_ICALL                                         $33     
         48        ASSIGN                                                   !4, $33
   70    49        INIT_FCALL                                               'userland_strrev'
         50        SEND_VAR                                                 !0
         51        DO_FCALL                                      0          
   71    52        INIT_FCALL                                               'microtime'
         53        SEND_VAL                                                 <true>
         54        DO_ICALL                                         $38     
         55        SUB                                              ~39     $38, !4
         56        FETCH_DIM_W                                      $36     !2, !5
         57        ASSIGN_DIM                                               $36
         58        OP_DATA                                                  ~39
   68    59        PRE_INC                                                  !3
         60    >   IS_SMALLER                                               !3, !1
         61      > JMPNZ                                                    ~41, ->45
   73    62    >   INIT_FCALL                                               'array_shift'
         63        FETCH_DIM_W                                      $42     !2, !5
         64        SEND_REF                                                 $42
         65        DO_ICALL                                                 
   74    66        INIT_FCALL                                               'array_pop'
         67        FETCH_DIM_W                                      $44     !2, !5
         68        SEND_REF                                                 $44
         69        DO_ICALL                                                 
   75    70        INIT_FCALL                                               'array_sum'
         71        FETCH_DIM_R                                      ~46     !2, !5
         72        SEND_VAL                                                 ~46
         73        DO_ICALL                                         $47     
         74        FETCH_DIM_R                                      ~48     !2, !5
         75        COUNT                                            ~49     ~48
         76        DIV                                              ~50     $47, ~49
         77        ASSIGN                                                   !6, ~50
   76    78        INIT_FCALL                                               'printf'
         79        SEND_VAL                                                 'Average+cost+for+function+userland_strrev%3A+%25.9f+s%0A'
         80        SEND_VAR                                                 !6
         81        DO_ICALL                                                 
         82      > 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/DK96q
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:
145.31 ms | 1407 KiB | 26 Q