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( 'FooBar', 'FooBAR', 'FOOBar', 'foo_bar', 'FOO_bar', 'FOO_Bar', 'foo_BAR', 'foo_Bar', 'Foo_bar', ); 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.0130.00618.16
8.3.50.0080.00822.03
8.3.40.0130.00318.94
8.3.30.0040.01119.22
8.3.20.0060.00320.38
8.3.10.0080.00022.02
8.3.00.0040.00418.02
8.2.180.0140.00716.88
8.2.170.0090.00922.96
8.2.160.0110.01120.57
8.2.150.0040.00424.18
8.2.140.0000.00824.66
8.2.130.0060.00926.16
8.2.120.0040.00421.00
8.2.110.0060.00322.25
8.2.100.0090.00318.34
8.2.90.0060.00319.47
8.2.80.0080.00017.97
8.2.70.0030.00617.88
8.2.60.0040.00418.05
8.2.50.0050.00318.07
8.2.40.0000.00818.47
8.2.30.0070.00018.46
8.2.20.0030.00617.96
8.2.10.0030.00618.22
8.2.00.0040.00417.82
8.1.280.0140.00025.92
8.1.270.0030.00523.97
8.1.260.0050.00326.35
8.1.250.0070.00028.09
8.1.240.0090.00023.91
8.1.230.0120.00019.14
8.1.220.0040.00417.91
8.1.210.0050.00318.77
8.1.200.0060.00317.38
8.1.190.0000.00817.36
8.1.180.0050.00318.10
8.1.170.0030.00518.76
8.1.160.0030.00522.23
8.1.150.0070.00018.91
8.1.140.0030.00517.55
8.1.130.0030.00318.00
8.1.120.0040.00417.61
8.1.110.0000.00817.62
8.1.100.0000.00817.52
8.1.90.0060.00317.59
8.1.80.0040.00417.63
8.1.70.0000.00717.45
8.1.60.0000.00817.71
8.1.50.0000.00817.54
8.1.40.0040.00417.67
8.1.30.0030.00517.73
8.1.20.0030.00517.68
8.1.10.0060.00317.56
8.1.00.0000.00817.64
8.0.300.0000.00718.77
8.0.290.0050.00216.75
8.0.280.0000.00718.56
8.0.270.0000.00716.99
8.0.260.0060.00017.36
8.0.250.0050.00317.15
8.0.240.0000.00817.04
8.0.230.0030.00517.16
8.0.220.0000.00717.07
8.0.210.0070.00017.06
8.0.200.0000.01017.12
8.0.190.0040.00417.10
8.0.180.0030.00517.04
8.0.170.0030.00517.00
8.0.160.0020.00517.08
8.0.150.0040.00416.90
8.0.140.0000.00916.93
8.0.130.0000.00613.46
8.0.120.0000.00817.08
8.0.110.0040.00417.04
8.0.100.0080.00017.05
8.0.90.0050.00316.86
8.0.80.0140.00317.02
8.0.70.0000.00717.04
8.0.60.0040.00417.02
8.0.50.0050.00317.07
8.0.30.0100.01017.25
8.0.20.0080.01117.40
8.0.10.0050.00317.05
8.0.00.0130.00616.95
7.4.330.0000.00515.15
7.4.320.0030.00316.59
7.4.300.0030.00316.51
7.4.290.0000.00716.52
7.4.280.0000.00716.62
7.4.270.0050.00316.66
7.4.260.0000.00716.49
7.4.250.0050.00216.66
7.4.240.0030.00516.62
7.4.230.0000.00716.45
7.4.220.0160.00316.61
7.4.210.0080.00616.71
7.4.200.0000.00716.50
7.4.160.0040.02016.61
7.4.150.0040.01317.40
7.4.140.0070.01017.86
7.4.130.0120.00716.72
7.4.120.0050.01216.62
7.4.110.0120.00616.73
7.4.100.0100.01316.66
7.4.90.0140.00616.76
7.4.80.0170.00619.39
7.4.70.0130.00816.71
7.4.60.0060.01016.70
7.4.50.0060.00316.70
7.4.40.0120.00616.60
7.4.30.0060.01016.65
7.4.00.0090.00715.01
7.3.330.0040.00413.34
7.3.320.0030.00313.50
7.3.310.0040.00416.43
7.3.300.0030.00316.44
7.3.290.0070.00716.39
7.3.280.0070.01216.46
7.3.270.0100.01717.40
7.3.260.0100.01016.40
7.3.250.0120.01216.47
7.3.240.0030.01416.44
7.3.230.0100.00716.71
7.3.210.0060.01516.49
7.3.200.0060.01519.39
7.3.190.0080.01116.44
7.3.180.0100.00616.57
7.3.170.0090.00916.51
7.3.160.0140.00316.35
7.3.120.0050.01014.85
7.3.110.0020.01414.76
7.3.100.0080.00614.89
7.3.90.0080.00614.94
7.3.80.0020.01014.86
7.3.70.0030.00915.01
7.3.60.0050.00814.89
7.3.50.0020.01214.77
7.3.40.0060.00714.83
7.3.30.0070.00614.71
7.3.20.0070.00616.59
7.3.10.0040.00916.69
7.3.00.0050.00616.58
7.2.330.0070.01616.83
7.2.320.0110.00616.90
7.2.310.0100.00716.77
7.2.300.0180.00716.83
7.2.290.0090.01216.66
7.2.250.0050.00915.14
7.2.240.0020.01215.05
7.2.230.0060.00515.31
7.2.220.0040.00915.34
7.2.210.0050.01114.93
7.2.200.0050.00915.23
7.2.190.0060.00415.29
7.2.180.0050.00914.90
7.2.170.0040.00615.12
7.2.160.0060.00615.18
7.2.150.0040.00816.81
7.2.140.0080.00416.96
7.2.130.0080.00417.02
7.2.120.0000.01116.97
7.2.110.0060.00717.11
7.2.100.0040.00916.68
7.2.90.0040.00817.02
7.2.80.0000.01116.95
7.2.70.0070.00716.77
7.2.60.0060.00617.09
7.2.50.0060.00617.02
7.2.40.0030.00716.99
7.2.30.0080.00417.05
7.2.20.0080.00416.68
7.2.10.0060.00617.03
7.2.00.0060.00718.25
7.1.330.0070.00615.72
7.1.320.0050.00715.84
7.1.310.0070.00815.95
7.1.300.0090.00415.74
7.1.290.0070.00515.66
7.1.280.0070.00315.84
7.1.270.0060.00415.68
7.1.260.0030.01015.76
7.1.250.0060.00615.61
7.1.240.0040.00715.85
7.1.230.0060.00615.92
7.1.220.0040.00715.89
7.1.210.0040.00715.96
7.1.200.0040.00815.76
7.1.190.0070.00515.78
7.1.180.0040.00715.92
7.1.170.0080.00415.78
7.1.160.0070.00415.88
7.1.150.0000.01215.96
7.1.140.0000.01115.68
7.1.130.0090.00315.60
7.1.120.0080.00415.89
7.1.110.0040.00815.95
7.1.100.0030.00917.09
7.1.90.0070.00415.98
7.1.80.0040.00715.63
7.1.70.0030.00716.50
7.1.60.0060.01217.47
7.1.50.0050.01216.43
7.1.40.0060.00615.93
7.1.30.0000.01315.86
7.1.20.0110.00015.67
7.1.10.0070.00415.60
7.1.00.0030.05019.16
7.0.330.0000.01115.22
7.0.320.0060.00615.43
7.0.310.0000.01115.53
7.0.300.0000.01115.14
7.0.290.0070.00415.43
7.0.280.0090.00315.43
7.0.270.0110.00015.39
7.0.260.0040.00815.20
7.0.250.0080.00415.56
7.0.240.0060.00615.50
7.0.230.0020.01015.57
7.0.220.0110.00015.21
7.0.210.0030.00915.59
7.0.200.0070.00316.05
7.0.190.0070.00615.11
7.0.180.0090.00315.61
7.0.170.0100.00215.23
7.0.160.0080.00415.47
7.0.150.0030.00915.27
7.0.140.0050.04018.70
7.0.130.0000.01115.21
7.0.120.0080.00415.63
7.0.110.0060.00615.16
7.0.100.0250.03717.84
7.0.90.0220.02617.74
7.0.80.0250.04517.73
7.0.70.0250.04017.67
7.0.60.0220.04517.83
7.0.50.0290.02717.87
7.0.40.0090.03716.88
7.0.30.0030.04716.74
7.0.20.0050.03016.62
7.0.10.0080.04316.73
7.0.00.0080.03416.78
5.6.400.0080.00414.82
5.6.390.0130.00014.69
5.6.380.0040.00814.32
5.6.370.0080.00414.45
5.6.360.0050.00914.59
5.6.350.0040.00914.81
5.6.340.0030.01014.82
5.6.330.0030.00914.45
5.6.320.0040.00814.75
5.6.310.0060.00614.66
5.6.300.0000.01314.69
5.6.290.0080.00414.11
5.6.280.0040.04017.75
5.6.270.0090.00414.46
5.6.260.0000.01314.77
5.6.250.0080.03717.57
5.6.240.0020.04917.61
5.6.230.0050.04617.69
5.6.220.0060.04617.58
5.6.210.0050.04817.61
5.6.200.0060.03517.82
5.6.190.0030.04217.80
5.6.180.0180.03317.90
5.6.170.0070.04517.79
5.6.160.0080.04217.86
5.6.150.0050.03017.85
5.6.140.0070.04517.80
5.6.130.0100.04117.83
5.6.120.0090.04517.78
5.6.110.0150.03717.86
5.6.100.0070.04317.78
5.6.90.0100.04017.82
5.6.80.0050.04317.53
5.6.70.0080.04217.52
5.6.60.0040.04917.53
5.6.50.0070.04417.35
5.6.40.0030.03117.54
5.6.30.0120.04017.35
5.6.20.0080.04217.54
5.6.10.0060.04017.47
5.6.00.0060.03517.25
5.5.380.0070.04317.45
5.5.370.0020.03117.32
5.5.360.0060.04017.31
5.5.350.0110.03617.52
5.5.340.0100.04417.64
5.5.330.0080.02717.65
5.5.320.0030.05017.88
5.5.310.0050.04617.59
5.5.300.0110.04017.87
5.5.290.0050.04317.75
5.5.280.0050.04617.77
5.5.270.0090.04417.78
5.5.260.0060.04617.74
5.5.250.0100.04117.46
5.5.240.0050.04317.52
5.5.230.0090.04217.29
5.5.220.0050.03017.29
5.5.210.0050.03817.42
5.5.200.0070.04317.24
5.5.190.0110.04217.19
5.5.180.0090.03417.41
5.5.170.0040.00814.59
5.5.160.0060.04217.29
5.5.150.0070.02417.29
5.5.140.0050.04117.43
5.5.130.0030.04117.42
5.5.120.0050.04617.13
5.5.110.0110.03717.30
5.5.100.0080.02717.09
5.5.90.0050.04517.29
5.5.80.0030.03617.23
5.5.70.0000.04917.31
5.5.60.0050.03717.31
5.5.50.0130.03217.14
5.5.40.0150.03517.24
5.5.30.0050.04317.32
5.5.20.0020.04617.25
5.5.10.0090.04117.34
5.5.00.0070.03617.02
5.4.450.0050.04315.46
5.4.440.0020.04715.29
5.4.430.0120.03315.33
5.4.420.0100.03815.35
5.4.410.0030.04615.27
5.4.400.0020.04515.26
5.4.390.0050.04115.23
5.4.380.0060.03115.13
5.4.370.0030.04315.12
5.4.360.0050.04115.19
5.4.350.0060.04115.04
5.4.340.0090.03715.12
5.4.330.0030.00611.22
5.4.320.0090.03715.12
5.4.310.0060.04115.19
5.4.300.0060.03815.17
5.4.290.0000.04515.17
5.4.280.0080.02715.18
5.4.270.0030.04015.16
5.4.260.0070.04015.29
5.4.250.0070.03515.15
5.4.240.0070.03115.03
5.4.230.0020.04315.12
5.4.220.0030.04315.12
5.4.210.0080.03815.12
5.4.200.0070.03615.19
5.4.190.0020.03315.16
5.4.180.0070.04115.05
5.4.170.0070.02015.16
5.4.160.0050.04115.11
5.4.150.0060.04115.04
5.4.140.0070.03713.80
5.4.130.0050.04013.81
5.4.120.0050.04113.80
5.4.110.0080.02713.88
5.4.100.0020.04113.85
5.4.90.0000.04313.79
5.4.80.0070.03413.91
5.4.70.0040.03713.86
5.4.60.0060.03913.83
5.4.50.0000.04313.91
5.4.40.0030.03613.90
5.4.30.0070.04013.92
5.4.20.0090.03413.89
5.4.10.0050.03513.76
5.4.00.0040.04113.57
5.3.290.0040.02713.04
5.3.280.0100.03912.99
5.3.270.0080.03212.99
5.3.260.0090.03512.95
5.3.250.0090.03513.01
5.3.240.0070.04012.99
5.3.230.0110.03313.00
5.3.220.0030.02612.94
5.3.210.0040.04212.99
5.3.200.0090.02912.93
5.3.190.0080.04212.95
5.3.180.0050.03912.97
5.3.170.0070.03213.00
5.3.160.0050.03912.98
5.3.150.0060.04012.99
5.3.140.0060.03912.93
5.3.130.0060.02012.93
5.3.120.0050.03912.92
5.3.110.0110.03312.99
5.3.100.0100.03412.67
5.3.90.0080.03712.68
5.3.80.0040.04212.75
5.3.70.0030.04412.74
5.3.60.0070.03812.75
5.3.50.0060.02212.61
5.3.40.0050.03812.70
5.3.30.0090.03012.64
5.3.20.0050.02112.52
5.3.10.0050.02112.51
5.3.00.0040.03712.55

preferences:
50.06 ms | 401 KiB | 5 Q