3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Str { public static $snakeCache = []; public static function snake($value, $delimiter = '_') { $key = $value; if (isset(static::$snakeCache[$key][$delimiter])) { return static::$snakeCache[$key][$delimiter]; } if (! ctype_lower($value)) { $value = preg_replace('/\s+/u', '', ucwords($value)); $value = static::lower(preg_replace('/(.)(?=[A-Z])/u', '$1'.$delimiter, $value)); } return static::$snakeCache[$key][$delimiter] = $value; } /** * Convert the given string to lower-case. * * @param string $value * @return string */ public static function lower($value) { return mb_strtolower($value, 'UTF-8'); } } // AaBbCc1, AaBbCc2, AaBbCc3, AaBbCc4,... と接頭辞が同じ文字列を1e5個用意します // この接頭辞付きの文字列群をスネークケースにすることを考えます $prefix = 'AaBbCc'; $strList = array_map(fn ($i) => $prefix.$i, range(1, 1e4)); $s = microtime(true); foreach ($strList as $str) { // 全てを直に変更するとキャッシュが使えないため処理が遅くなります // \Str::snake($str); // 変換したい共通部である接頭辞を分離して、そこだけ変換にかけます。 // するとキャッシュが有効になり高速化します。 // ついでに巨大なキャッシュ配列が生成されることもなくなり、メモリにも優しいです。 [$prefix, $idx] = str_split($str, 6); \Str::snake($prefix).$idx; } echo microtime(true) - $s;
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 17, Position 2 = 33
Branch analysis from position: 17
2 jumps found. (Code = 78) Position 1 = 18, Position 2 = 33
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 17
Branch analysis from position: 17
Branch analysis from position: 33
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 33
filename:       /in/RmEKI
function name:  (null)
number of ops:  40
compiled vars:  !0 = $prefix, !1 = $strList, !2 = $s, !3 = $str, !4 = $idx
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   37     0  E >   ASSIGN                                                   !0, 'AaBbCc'
   38     1        INIT_FCALL                                               'array_map'
          2        DECLARE_LAMBDA_FUNCTION                          ~6      [0]
          3        BIND_LEXICAL                                             ~6, !0
          4        SEND_VAL                                                 ~6
          5        INIT_FCALL                                               'range'
          6        SEND_VAL                                                 1
          7        SEND_VAL                                                 10000
          8        DO_ICALL                                         $7      
          9        SEND_VAR                                                 $7
         10        DO_ICALL                                         $8      
         11        ASSIGN                                                   !1, $8
   40    12        INIT_FCALL                                               'microtime'
         13        SEND_VAL                                                 <true>
         14        DO_ICALL                                         $10     
         15        ASSIGN                                                   !2, $10
   41    16      > FE_RESET_R                                       $12     !1, ->33
         17    > > FE_FETCH_R                                               $12, !3, ->33
   47    18    >   INIT_FCALL                                               'str_split'
         19        SEND_VAR                                                 !3
         20        SEND_VAL                                                 6
         21        DO_ICALL                                         $13     
         22        FETCH_LIST_R                                     $14     $13, 0
         23        ASSIGN                                                   !0, $14
         24        FETCH_LIST_R                                     $16     $13, 1
         25        ASSIGN                                                   !4, $16
         26        FREE                                                     $13
   48    27        INIT_STATIC_METHOD_CALL                                  'Str', 'snake'
         28        SEND_VAR                                                 !0
         29        DO_FCALL                                      0  $18     
         30        CONCAT                                           ~19     $18, !4
         31        FREE                                                     ~19
   41    32      > JMP                                                      ->17
         33    >   FE_FREE                                                  $12
   50    34        INIT_FCALL                                               'microtime'
         35        SEND_VAL                                                 <true>
         36        DO_ICALL                                         $20     
         37        SUB                                              ~21     $20, !2
         38        ECHO                                                     ~21
   51    39      > RETURN                                                   1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/RmEKI
function name:  {closure}
number of ops:  5
compiled vars:  !0 = $i, !1 = $prefix
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   38     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
          2        CONCAT                                           ~2      !1, !0
          3      > RETURN                                                   ~2
          4*     > RETURN                                                   null

End of Dynamic Function 0

Class Str:
Function snake:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 11
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 35
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 35
filename:       /in/RmEKI
function name:  snake
number of ops:  41
compiled vars:  !0 = $value, !1 = $delimiter, !2 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      '_'
    9     2        ASSIGN                                                   !2, !0
   11     3        FETCH_STATIC_PROP_IS                             ~4      'snakeCache'
          4        FETCH_DIM_IS                                     ~5      ~4, !2
          5        ISSET_ISEMPTY_DIM_OBJ                         0          ~5, !1
          6      > JMPZ                                                     ~6, ->11
   12     7    >   FETCH_STATIC_PROP_R          unknown             ~7      'snakeCache'
          8        FETCH_DIM_R                                      ~8      ~7, !2
          9        FETCH_DIM_R                                      ~9      ~8, !1
         10      > RETURN                                                   ~9
   15    11    >   INIT_FCALL                                               'ctype_lower'
         12        SEND_VAR                                                 !0
         13        DO_ICALL                                         $10     
         14        BOOL_NOT                                         ~11     $10
         15      > JMPZ                                                     ~11, ->35
   16    16    >   INIT_FCALL                                               'preg_replace'
         17        SEND_VAL                                                 '%2F%5Cs%2B%2Fu'
         18        SEND_VAL                                                 ''
         19        INIT_FCALL                                               'ucwords'
         20        SEND_VAR                                                 !0
         21        DO_ICALL                                         $12     
         22        SEND_VAR                                                 $12
         23        DO_ICALL                                         $13     
         24        ASSIGN                                                   !0, $13
   18    25        INIT_STATIC_METHOD_CALL                                  'lower'
         26        INIT_FCALL                                               'preg_replace'
         27        SEND_VAL                                                 '%2F%28.%29%28%3F%3D%5BA-Z%5D%29%2Fu'
         28        CONCAT                                           ~15     '%241', !1
         29        SEND_VAL                                                 ~15
         30        SEND_VAR                                                 !0
         31        DO_ICALL                                         $16     
         32        SEND_VAR_NO_REF_EX                                       $16
         33        DO_FCALL                                      0  $17     
         34        ASSIGN                                                   !0, $17
   21    35    >   FETCH_STATIC_PROP_W          unknown             $19     'snakeCache'
         36        FETCH_DIM_W                                      $20     $19, !2
         37        ASSIGN_DIM                                       ~21     $20, !1
         38        OP_DATA                                                  !0
         39      > RETURN                                                   ~21
   22    40*     > RETURN                                                   null

End of function snake

Function lower:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/RmEKI
function name:  lower
number of ops:  7
compiled vars:  !0 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   RECV                                             !0      
   32     1        INIT_FCALL                                               'mb_strtolower'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 'UTF-8'
          4        DO_ICALL                                         $1      
          5      > RETURN                                                   $1
   33     6*     > RETURN                                                   null

End of function lower

End of class Str.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
152.17 ms | 1476 KiB | 21 Q