3v4l.org

run code in 300+ PHP versions simultaneously
<?php function do_split($name) { return preg_split('/([A-Z]{2,}|([A-Z][^A-Z]*))/', $name, null, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); } function convert_case($name) { return implode( '_', array_map( 'strtolower', do_split($name) ) ); } function convert_case2($in) { $len = strlen($in); if ($len < 2) { return strtolower($in); } // no state $upper = function ($c) { return 'A' <= $c && $c <= 'Z'; }; $lower = function ($c) { return 'a' <= $c && $c <= 'z'; }; $alpha = function ($c) use ($upper, $lower) { return $upper($c) || $lower($c); }; $num = function ($c) { return '0' <= $c && $c <= '9'; }; $alpha_num = function ($c) use ($alpha, $num) { return $alpha($c) || $num($c); }; // stream state $pos = 0; $char = $in[0]; $peek = function() use ($in, &$pos, $len) { if ($pos+1 >= $len) { return null; } return $in[$pos+1]; }; $next = function() use ($peek, &$pos, &$char) { $char = $peek(); $pos++; return $char !== null; }; // lex state $out = array(); $std_word = null; $upr_word = null; $num_word = null; $find_word = function () use (&$char, $alpha_num, $lower, $upper, $next, $peek, &$std_word, &$upr_word, &$num_word) { while (!$alpha_num($char)) { if (!$next()) { return null; } } if ($lower($char)) { return $std_word; // lowercase word } if ($upper($char)) { if ($upper($peek())) { // uppercase word return $upr_word; } return $std_word; // capitalized word } return $num_word; }; $upr_word = function() use (&$out, &$char, $next, $peek, $upper, $lower, $find_word) { $word = $char; while ($next() && $upper($char)) { if ($lower($peek())) { // start of capitalized word (e.g. FOOBar at B) break; } $word .= $char; } $out[] = $word; return $find_word; }; $std_word = function() use (&$out, &$char, $next, $lower, $find_word) { $word = $char; while ($next() && $lower($char)) { $word .= $char; } $out[] = $word; return $find_word; }; $num_word = function() use (&$out, &$char, $next, $num, $find_word) { $word = $char; while ($next() && $num($char)) { $word .= $char; } $out[] = $word; return $find_word; }; $state = $find_word; while ($state !== null) { $state = $state(); } return strtolower(implode('_', $out)); } $samples = array( 'FooBar123', 'FooBAR123', 'FOOBar123', 'foo_bar_123', 'FOO_bar_123', 'FOO_Bar_123', 'foo_BAR_123', 'foo_Bar_123', 'Foo_bar_123', ); print_r(array_map(function ($name) { return array( 'name' => $name, 'converted' => convert_case2($name), // 'converted' => convert_case($name), ); }, $samples));

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.3.60.0160.00018.31
8.3.50.0100.00622.10
8.3.40.0130.00318.79
8.3.30.0060.00919.02
8.3.20.0040.01220.44
8.3.10.0050.00321.74
8.3.00.0100.00718.03
8.2.180.0160.00016.63
8.2.170.0090.00922.96
8.2.160.0120.00920.52
8.2.150.0000.00824.18
8.2.140.0000.00824.66
8.2.130.0080.00026.16
8.2.120.0060.00321.00
8.2.110.0070.00322.13
8.2.100.0080.00418.28
8.2.90.0000.00919.48
8.2.80.0040.00417.97
8.2.70.0060.00317.86
8.2.60.0060.00318.18
8.2.50.0000.00818.07
8.2.40.0050.00318.35
8.2.30.0030.00618.33
8.2.20.0030.00517.85
8.2.10.0050.00318.29
8.2.00.0060.00317.97
8.1.280.0040.01125.92
8.1.270.0040.00423.85
8.1.260.0000.00726.35
8.1.250.0040.00428.09
8.1.240.0040.00423.75
8.1.230.0060.00619.16
8.1.220.0060.00317.78
8.1.210.0080.00018.77
8.1.200.0000.00917.38
8.1.190.0030.00617.34
8.1.180.0030.00518.10
8.1.170.0040.00418.67
8.1.160.0040.00422.01
8.1.150.0000.00718.93
8.1.140.0000.00717.52
8.1.130.0040.00417.94
8.1.120.0000.00717.63
8.1.110.0040.00417.63
8.1.100.0040.00417.57
8.1.90.0000.00717.57
8.1.80.0000.00817.57
8.1.70.0000.00717.57
8.1.60.0080.00017.59
8.1.50.0030.00617.56
8.1.40.0040.00417.65
8.1.30.0090.00017.79
8.1.20.0000.00817.73
8.1.10.0040.00417.70
8.1.00.0060.00317.62
8.0.300.0090.00018.77
8.0.290.0040.00416.63
8.0.280.0030.00318.55
8.0.270.0040.00417.28
8.0.260.0000.00717.27
8.0.250.0070.00017.00
8.0.240.0070.00017.11
8.0.230.0000.00717.15
8.0.220.0000.00717.05
8.0.210.0030.00317.07
8.0.200.0040.00417.08
8.0.190.0040.00417.05
8.0.180.0040.00416.94
8.0.170.0060.00317.14
8.0.160.0000.00717.11
8.0.150.0080.00016.90
8.0.140.0070.00317.01
8.0.130.0030.00313.43
8.0.120.0000.01017.02
8.0.110.0050.00317.01
8.0.100.0000.00717.08
8.0.90.0000.00816.85
8.0.80.0160.00617.03
8.0.70.0040.00417.18
8.0.60.0040.00417.05
8.0.50.0050.00317.12
8.0.30.0080.01117.31
8.0.20.0070.01317.40
8.0.10.0000.00717.14
8.0.00.0080.01017.02
7.4.330.0000.00515.15
7.4.320.0030.00316.63
7.4.300.0060.00016.61
7.4.290.0030.00316.61
7.4.280.0030.00316.76
7.4.270.0030.00316.71
7.4.260.0030.00316.69
7.4.250.0070.00016.72
7.4.240.0020.00616.63
7.4.230.0050.00316.60
7.4.220.0090.01016.63
7.4.210.0060.01016.78
7.4.200.0000.00816.79
7.4.160.0060.01216.64
7.4.150.0060.01217.40
7.4.140.0080.00917.86
7.4.130.0110.01016.58
7.4.120.0080.01016.63
7.4.110.0070.01016.54
7.4.100.0070.01716.59
7.4.90.0090.00916.72
7.4.80.0150.00319.39
7.4.70.0100.01316.71
7.4.60.0090.00616.54
7.4.50.0080.00016.75
7.4.40.0060.01016.60
7.4.30.0070.01016.45
7.4.00.0100.00315.09
7.3.330.0030.00313.43
7.3.320.0050.00013.34
7.3.310.0000.00716.44
7.3.300.0070.00016.38
7.3.290.0130.00716.43
7.3.280.0060.01516.47
7.3.270.0110.01217.40
7.3.260.0070.01016.32
7.3.250.0100.01216.44
7.3.240.0100.01316.54
7.3.230.0030.01416.71
7.3.210.0140.00316.48
7.3.200.0140.00419.39
7.3.190.0030.01416.78
7.3.180.0130.00616.56
7.3.170.0060.01516.49
7.3.160.0070.01016.77
7.3.120.0030.01014.70
7.2.330.0140.00316.66
7.2.320.0090.00916.84
7.2.310.0130.00316.79
7.2.300.0150.00316.95
7.2.290.0070.01017.03
7.2.00.0070.01019.47
7.1.100.0060.00918.12
7.1.70.0000.00817.14
7.1.60.0100.01619.25
7.1.50.0040.01816.88
7.1.00.0070.07322.43
7.0.200.0000.00916.71
7.0.140.0000.07722.07
7.0.100.0170.07020.05
7.0.90.0170.08019.95
7.0.80.0200.07319.97
7.0.70.0130.08319.99
7.0.60.0130.08020.05
7.0.50.0070.08320.36
7.0.40.0100.08320.02
7.0.30.0100.05320.03
7.0.20.0000.05020.13
7.0.10.0070.06020.02
7.0.00.0030.04320.09
5.6.280.0030.07721.12
5.6.250.0070.08720.78
5.6.240.0030.09320.61
5.6.230.0070.07720.75
5.6.220.0070.07320.66
5.6.210.0070.07720.69
5.6.200.0070.08721.05
5.6.190.0070.08721.08
5.6.180.0170.06721.11
5.6.170.0070.09021.04
5.6.160.0030.04321.02
5.6.150.0030.05321.12
5.6.140.0100.03721.02
5.6.130.0130.04321.13
5.6.120.0130.03721.05
5.6.110.0030.04320.94
5.6.100.0070.04021.04
5.6.90.0070.06021.09
5.6.80.0130.05720.35
5.6.70.0100.04320.43
5.6.60.0030.05320.54
5.6.50.0030.04020.42
5.6.40.0070.03720.38
5.6.30.0030.04720.38
5.6.20.0030.04020.39
5.6.10.0000.04020.44
5.6.00.0030.04020.29
5.5.380.0130.07720.38
5.5.370.0100.07320.55
5.5.360.0030.09320.39
5.5.350.0200.07020.34
5.5.340.0100.07320.78
5.5.330.0170.04320.86
5.5.320.0070.08020.77
5.5.310.0030.06720.63
5.5.300.0000.05720.85
5.5.290.0070.04020.92
5.5.280.0070.07320.82
5.5.270.0130.04320.82
5.5.260.0070.04020.82
5.5.250.0130.05020.59
5.5.240.0070.07320.27
5.5.230.0030.06020.18
5.5.220.0030.04320.16
5.5.210.0030.04020.02
5.5.200.0000.04320.25
5.5.190.0130.03320.14
5.5.180.0070.03720.13
5.5.160.0070.03720.21
5.5.150.0030.04020.24
5.5.140.0030.04020.07
5.5.130.0030.07320.23
5.5.120.0030.04020.22
5.5.110.0030.04320.18
5.5.100.0000.04320.05
5.5.90.0070.03720.07
5.5.80.0070.03720.06
5.5.70.0030.04020.14
5.5.60.0030.04320.11
5.5.50.0030.03720.05
5.5.40.0070.03320.13
5.5.30.0100.06020.15
5.5.20.0070.05720.06
5.5.10.0100.07320.12
5.5.00.0130.07719.97
5.4.450.0100.03319.38
5.4.440.0100.03019.51
5.4.430.0000.04719.18
5.4.420.0070.05319.20
5.4.410.0000.04319.24
5.4.400.0030.06018.89
5.4.390.0000.04019.05
5.4.380.0070.04719.04
5.4.370.0100.03318.91
5.4.360.0030.03719.04
5.4.350.0070.03719.01
5.4.340.0000.04719.14
5.4.320.0000.04719.10
5.4.310.0000.04019.17
5.4.300.0070.03318.95
5.4.290.0100.04719.16
5.4.280.0100.06019.14
5.4.270.0070.04318.92
5.4.260.0030.03718.87
5.4.250.0100.03719.05
5.4.240.0000.05319.04
5.4.230.0000.04318.90
5.4.220.0000.04019.13
5.4.210.0030.07019.18
5.4.200.0070.05719.14
5.4.190.0000.04718.84
5.4.180.0030.03719.13
5.4.170.0130.07319.04
5.4.160.0100.07319.02
5.4.150.0170.04019.12
5.4.140.0130.06716.45
5.4.130.0130.06716.45
5.4.120.0070.07316.45
5.4.110.0170.03716.45
5.4.100.0100.06316.57
5.4.90.0100.05716.57
5.4.80.0100.07016.47
5.4.70.0030.07316.44
5.4.60.0170.06016.43
5.4.50.0070.08016.53
5.4.40.0130.05716.54
5.4.30.0000.07316.35
5.4.20.0100.07016.45
5.4.10.0100.06716.30
5.4.00.0000.07315.91
5.3.290.0030.04314.89
5.3.280.0070.03314.81
5.3.270.0100.07714.73
5.3.260.0070.04314.88
5.3.250.0170.06314.70
5.3.240.0170.05714.77
5.3.230.0070.07314.90
5.3.220.0130.06714.72
5.3.210.0130.06014.79
5.3.200.0070.07314.72
5.3.190.0170.06714.76
5.3.180.0000.08014.86
5.3.170.0170.06714.84
5.3.160.0100.07714.84
5.3.150.0070.08014.71
5.3.140.0070.06714.64
5.3.130.0100.07714.87
5.3.120.0070.07314.64
5.3.110.0100.07714.85
5.3.100.0130.07014.18
5.3.90.0070.07714.23
5.3.80.0130.06714.20
5.3.70.0070.08014.21
5.3.60.0000.08714.28
5.3.50.0070.07314.20
5.3.40.0030.07714.09
5.3.30.0130.06314.13
5.3.20.0070.07313.77
5.3.10.0100.06713.91
5.3.00.0070.07713.86

preferences:
45.88 ms | 401 KiB | 5 Q