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); } $pos = 0; $out = ''; $peek = function() { if ($pos+1 >= $len) { return null; } return $in[$pos+1] } $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); } $start_state = function () { return null; } $state = $start_state; while ($state !== null) { $state = $state() } return strtolower($output); } convert_case2('FooBARbar0123'); $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)
5.4.310.0100.03612.52
5.4.300.0060.03712.52
5.4.290.0010.04212.52
5.4.280.0040.03912.42
5.4.270.0090.03412.41
5.4.260.0060.03812.42
5.4.250.0040.04012.41
5.4.240.0060.03612.41
5.4.230.0070.04412.41
5.4.220.0040.04212.41
5.4.210.0060.03712.41
5.4.200.0090.03612.41
5.4.190.0030.03912.40
5.4.180.0050.03712.40
5.4.170.0060.04112.41
5.4.160.0070.03612.40
5.4.150.0080.03412.40
5.4.140.0050.03812.09
5.4.130.0050.03712.07
5.4.120.0050.03612.04
5.4.110.0060.03612.04
5.4.100.0050.03612.03
5.4.90.0040.03912.04
5.4.80.0090.03412.04
5.4.70.0080.03412.03
5.4.60.0060.03512.04
5.4.50.0090.03212.03
5.4.40.0050.03612.02
5.4.30.0070.03512.01
5.4.20.0060.03512.01
5.4.10.0050.03612.02
5.4.00.0040.03811.51
5.3.280.0060.03812.71
5.3.270.0080.03912.73
5.3.260.0050.04112.72
5.3.250.0080.03712.72
5.3.240.0090.03612.72
5.3.230.0110.03612.70
5.3.220.0060.03712.68
5.3.210.0060.04112.68
5.3.200.0050.03912.68
5.3.190.0080.03712.68
5.3.180.0030.04012.67
5.3.170.0110.03112.67
5.3.160.0080.03512.67
5.3.150.0060.03912.68
5.3.140.0060.03812.66
5.3.130.0020.04312.66
5.3.120.0040.04112.66
5.3.110.0130.04112.66
5.3.100.0020.04312.13
5.3.90.0060.03512.11
5.3.80.0090.03612.11
5.3.70.0070.03612.11
5.3.60.0020.04012.09
5.3.50.0060.03612.04
5.3.40.0070.03512.04
5.3.30.0080.03212.00
5.3.20.0040.03811.78
5.3.10.0080.03311.74
5.3.00.0040.03911.75
5.2.170.0070.0299.23
5.2.160.0050.0299.23
5.2.150.0050.0309.23
5.2.140.0060.0309.22
5.2.130.0040.0319.18
5.2.120.0080.0279.19
5.2.110.0050.0339.19
5.2.100.0060.0289.18
5.2.90.0060.0339.18
5.2.80.0020.0339.18
5.2.70.0020.0339.18
5.2.60.0010.0349.15
5.2.50.0030.0329.12
5.2.40.0030.0309.09
5.2.30.0070.0279.06
5.2.20.0030.0309.05
5.2.10.0060.0268.95
5.2.00.0050.0308.82
5.1.60.0030.0258.10
5.1.50.0050.0258.10
5.1.40.0040.0258.08
5.1.30.0030.0278.42
5.1.20.0040.0268.45
5.1.10.0050.0258.18
5.1.00.0050.0258.18
5.0.50.0050.0206.65
5.0.40.0030.0206.52
5.0.30.0040.0316.32
5.0.20.0020.0206.29
5.0.10.0030.0206.27
5.0.00.0030.0316.27
4.4.90.0030.0154.78
4.4.80.0020.0164.75
4.4.70.0030.0154.75
4.4.60.0020.0174.75
4.4.50.0040.0154.77
4.4.40.0020.0264.71
4.4.30.0040.0144.76
4.4.20.0010.0174.84
4.4.10.0010.0184.85
4.4.00.0020.0264.76
4.3.110.0040.0154.67
4.3.100.0020.0164.66
4.3.90.0040.0134.64
4.3.80.0040.0234.58
4.3.70.0030.0144.63
4.3.60.0040.0134.63
4.3.50.0040.0144.62
4.3.40.0000.0274.54
4.3.30.0020.0193.31
4.3.20.0030.0153.29
4.3.10.0040.0133.25
4.3.00.0070.02015.25

preferences:
143.27 ms | 1386 KiB | 7 Q