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;

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.1.100.0100.01018.82
8.1.90.0110.01118.79
8.1.80.0110.01118.67
8.1.70.0100.01518.74
8.1.60.0120.01218.75
8.1.50.0040.01618.78
8.1.40.0090.01218.74
8.1.30.0120.00918.99
8.1.20.0070.01318.96
8.1.10.0040.01618.77
8.1.00.0090.01318.81
8.0.230.0080.01418.28
8.0.220.0120.01218.21
8.0.210.0160.00818.28
8.0.200.0140.01018.26
8.0.190.0090.01418.37
8.0.180.0130.01218.24
8.0.170.0090.01318.20
8.0.160.0040.01818.16
8.0.150.0090.01318.21
8.0.140.0150.00618.23
8.0.130.0180.00318.05
8.0.120.0120.00818.28
8.0.110.0090.01218.24
8.0.100.0070.01418.23
8.0.90.0060.01718.16
8.0.80.0130.00718.25
8.0.70.0090.01318.05
8.0.60.0110.01118.15
8.0.50.0110.01118.23
8.0.30.0080.01318.25
8.0.20.0110.01118.32
8.0.10.0060.01718.35
7.4.300.0120.01217.97
7.4.290.0120.01018.02
7.4.280.0170.00617.90
7.4.270.0130.01318.03
7.4.260.0100.01418.00
7.4.250.0170.00617.99
7.4.240.0060.01217.93
7.4.230.0000.01717.95
7.4.220.0060.01017.98
7.4.210.0130.00817.92
7.4.200.0030.01318.04
7.4.190.0040.01318.00
7.4.180.0040.01318.02
7.4.160.0070.01017.95
7.4.150.0100.00717.93
7.4.140.0090.00917.85
7.4.130.0070.01117.88
7.4.120.0120.00917.87
7.4.110.0130.00817.79
7.4.100.0030.01217.94
7.4.90.0000.01817.79
7.4.80.0080.01217.92
7.4.70.0070.01117.88
7.4.60.0050.00917.93
7.4.50.0130.00517.82
7.4.40.0000.01617.84
7.4.30.0080.00817.80
7.4.20.0100.00717.69
7.4.10.0120.00417.86
7.4.00.0000.01617.92

preferences:
41.48 ms | 403 KiB | 5 Q