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;
Output for 8.1.10
0.0034909248352051
Output for 8.1.9
0.0026500225067139
Output for 8.1.8
0.0025839805603027
Output for 8.1.7
0.0037531852722168
Output for 8.1.6
0.0025911331176758
Output for 8.1.5
0.0024549961090088
Output for 8.1.4
0.0025420188903809
Output for 8.1.3
0.0026919841766357
Output for 8.1.2
0.0025110244750977
Output for 8.1.1
0.0025959014892578
Output for 8.1.0
0.0024960041046143
Output for 8.0.23
0.0025060176849365
Output for 8.0.22
0.0025920867919922
Output for 8.0.21
0.0025548934936523
Output for 8.0.20
0.0024421215057373
Output for 8.0.19
0.0027389526367188
Output for 8.0.18
0.0025019645690918
Output for 8.0.17
0.0024650096893311
Output for 8.0.16
0.0025551319122314
Output for 8.0.15
0.0024471282958984
Output for 8.0.14
0.0025880336761475
Output for 8.0.13
0.0025689601898193
Output for 8.0.12
0.0024302005767822
Output for 7.4.25, 8.0.11
0.0025858879089355
Output for 8.0.10
0.0026688575744629
Output for 8.0.9
0.0025291442871094
Output for 8.0.8
0.0024459362030029
Output for 8.0.7
0.0024940967559814
Output for 8.0.6
0.0025589466094971
Output for 8.0.5
0.0025429725646973
Output for 8.0.3
0.0024189949035645
Output for 8.0.2
0.002471923828125
Output for 8.0.1
0.0025248527526855
Output for 7.4.30
0.0029881000518799
Output for 7.4.29
0.0028409957885742
Output for 7.4.28
0.0026431083679199
Output for 7.4.27
0.0026600360870361
Output for 7.4.26
0.0027880668640137
Output for 7.4.24
0.0040910243988037
Output for 7.4.23
0.0033309459686279
Output for 7.4.22
0.0043458938598633
Output for 7.4.21
0.0026810169219971
Output for 7.4.20
0.0034041404724121
Output for 7.4.19
0.0040040016174316
Output for 7.4.18
0.0038638114929199
Output for 7.4.16
0.004270076751709
Output for 7.4.15
0.0041618347167969
Output for 7.4.14
0.0039970874786377
Output for 7.4.13
0.0050010681152344
Output for 7.4.12
0.0026140213012695
Output for 7.4.11
0.0025579929351807
Output for 7.4.10
0.0045430660247803
Output for 7.4.9
0.0025608539581299
Output for 7.4.8
0.0025458335876465
Output for 7.4.7
0.0025308132171631
Output for 7.4.6
0.0047202110290527
Output for 7.4.5
0.0067489147186279
Output for 7.4.4
0.003849983215332
Output for 7.4.3
0.0042998790740967
Output for 7.4.2
0.0039770603179932
Output for 7.4.1
0.0037379264831543
Output for 7.4.0
0.0042579174041748

preferences:
62.49 ms | 488 KiB | 5 Q