3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Brace Balance Check * * Function which checks whether or not the braces in an array of strings are balanced * @param array $values an array of strings containing braces * @return array $values an array of strings containing YES or NO responses */ function braceBalanceCheck(array $values) : array { for ($i = 0; $i <= count($values) - 1; $i++) { $braces = str_split($values[$i]); $values[$i] = isValid($braces); } return $values; } function isValid(array $braces) : string { $available = ['{' => '}', '(' => ')', '[' => ']']; $opened = []; $closed = []; for ($i = 0; $i <= count($braces) - 1; $i++) { if (in_array($braces[$i], array_keys($available))) { $opened[] = $braces[$i]; continue; } $closed[] = $braces[$i]; } return match($braces, $available, $opened, $closed); } function match(array $braces, array $available, array $opened, array $closed) : string { $last_open = $opened[count($opened) - 1]; $opened = array_values($opened); $closed = array_values($closed); if (!testValidity($braces, $available, $last_open, $opened, $closed)) { return 'NO'; } if (count($opened) > 1) { match($braces, $available, array_slice($opened, 0, -1) , array_slice($closed, 1)); } return 'YES'; } function testValidity(array $braces, array $available, string $last_open, array $opened, array $closed) : bool { // Check for unopened braces $is_opened = in_array( array_flip($available)[$closed[0]], array_slice($braces, 0, array_search($closed[0], $braces)) ); if (!$is_opened) { return false; } // check if not nested properly if ($available[$last_open] !== $closed[0]) { // check if consecutive if ($available[$opened[0]] === $closed[0]) { return true; } return false; } // check if not consecutive if ($available[$opened[0]] !== $closed[0]) { // check if nested properly if ($available[$last_open] === $closed[0]) { return true; } return false; } return true; } var_dump(braceBalanceCheck(['{[()]}', '{[(]}', '{}()[]', '{]}[', '{}{'])); // YES, NO, YES, NO

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.0110.01118.68
8.3.50.0040.01116.51
8.3.40.0110.00418.70
8.3.30.0110.00418.71
8.3.20.0040.00418.98
8.3.10.0080.00020.93
8.3.00.0040.00417.91
8.2.180.0120.00616.38
8.2.170.0150.00022.96
8.2.160.0060.00920.82
8.2.150.0040.00424.18
8.2.140.0050.00324.66
8.2.130.0040.00419.70
8.2.120.0030.00526.35
8.2.110.0060.00321.13
8.2.100.0040.00817.78
8.2.90.0000.00819.47
8.2.80.0030.00618.61
8.2.70.0000.00817.66
8.2.60.0030.00617.92
8.2.50.0040.00418.10
8.2.40.0040.00420.95
8.2.30.0000.00719.20
8.2.20.0040.00417.96
8.2.10.0000.00817.77
8.2.00.0030.00519.23
8.1.280.0070.00725.92
8.1.270.0080.00023.81
8.1.260.0040.00426.35
8.1.250.0000.00828.09
8.1.240.0030.00624.01
8.1.230.0000.01222.85
8.1.220.0000.00817.74
8.1.210.0040.00418.77
8.1.200.0080.00017.22
8.1.190.0040.00417.10
8.1.180.0020.00518.10
8.1.170.0000.00818.46
8.1.160.0000.00718.73
8.1.150.0050.00218.74
8.1.140.0070.00017.54
8.1.130.0030.00318.81
8.1.120.0070.00317.23
8.1.110.0040.00417.21
8.1.100.0050.00217.23
8.1.90.0060.00317.20
8.1.80.0050.00217.20
8.1.70.0000.00717.25
8.1.60.0000.00717.46
8.1.50.0000.00717.34
8.1.40.0050.00217.39
8.1.30.0040.00417.56
8.1.20.0000.00817.50
8.1.10.0040.00417.43
8.1.00.0030.00617.20
8.0.300.0060.00321.64
8.0.290.0000.00816.63
8.0.280.0030.00318.25
8.0.270.0030.00317.15
8.0.260.0000.00718.34
8.0.250.0030.00316.95
8.0.240.0000.00716.91
8.0.230.0040.00416.80
8.0.220.0030.00316.84
8.0.210.0000.00716.75
8.0.200.0070.00016.91
8.0.190.0000.00816.79
8.0.180.0000.00716.80
8.0.170.0060.00316.79
8.0.160.0040.00416.91
8.0.150.0030.00316.69
8.0.140.0000.00816.86
8.0.130.0030.00313.48
8.0.120.0080.00016.84
8.0.110.0030.00316.80
8.0.100.0000.00716.73
8.0.90.0000.00716.82
8.0.80.0130.00916.91
8.0.70.0040.00316.84
8.0.60.0000.00716.81
8.0.50.0030.00516.88
8.0.30.0090.01116.90
8.0.20.0120.00617.40
8.0.10.0070.00016.91
8.0.00.0100.00816.50
7.4.330.0000.00415.55
7.4.320.0060.00016.45
7.4.300.0030.00316.65
7.4.290.0030.00316.56
7.4.280.0000.00816.59
7.4.270.0050.00516.68
7.4.260.0040.00816.55
7.4.250.0000.00716.59
7.4.240.0000.00716.64
7.4.230.0040.00416.71
7.4.220.0000.00816.72
7.4.210.0080.01116.54
7.4.200.0030.00316.35
7.4.160.0110.00816.60
7.4.140.0160.01117.86
7.4.130.0060.01216.68
7.4.120.0110.01316.64
7.4.110.0140.00316.66
7.4.100.0080.01116.57
7.4.90.0060.01316.40
7.4.80.0120.00519.39
7.4.70.0080.00816.63
7.4.60.0070.01116.52
7.4.50.0080.00616.78
7.4.40.0070.01316.55
7.4.00.0070.01215.03
7.3.330.0030.00313.50
7.3.320.0080.00013.44
7.3.310.0070.00016.43
7.3.300.0000.00716.50
7.3.290.0040.00416.49
7.3.280.0110.00816.47
7.3.260.0130.00516.58
7.3.240.0080.00916.53
7.3.230.0100.00716.70
7.3.210.0060.01216.41
7.3.200.0040.01916.59
7.3.190.0060.00916.34
7.3.180.0100.00716.66
7.3.170.0170.00716.64
7.3.160.0100.00616.46
7.3.120.0100.00714.79
7.3.110.0070.01014.95
7.3.100.0050.01015.02
7.3.90.0090.00414.98
7.3.80.0050.01014.93
7.3.70.0040.01014.96
7.3.60.0070.00814.85
7.3.50.0090.00714.92
7.3.40.0080.00714.92
7.3.30.0050.01114.98
7.3.20.0080.00616.80
7.3.10.0080.00716.68
7.3.00.0050.01016.66
7.2.330.0060.01216.77
7.2.320.0120.01216.77
7.2.310.0140.00816.65
7.2.300.0070.01016.86
7.2.290.0050.01416.55
7.2.250.0050.01115.28
7.2.240.0100.00715.07
7.2.230.0080.00815.17
7.2.220.0090.00715.18
7.2.210.0070.00915.07
7.2.200.0030.01115.16
7.2.190.0050.01115.10
7.2.180.0080.00815.12
7.2.170.0080.00715.05
7.2.160.0050.01215.10
7.2.150.0040.01216.90
7.2.140.0050.00716.83
7.2.130.0030.01016.93
7.2.120.0100.00516.92
7.2.110.0100.00516.81
7.2.100.0070.00716.95
7.2.90.0040.01017.03
7.2.80.0020.01417.00
7.2.70.0070.00716.95
7.2.60.0290.01016.35
7.2.50.0310.01016.38
7.2.40.0340.01016.53
7.2.30.0360.00816.48
7.2.20.0340.00816.59
7.2.10.0320.00916.36
7.2.00.0310.00716.42
7.1.330.0080.00815.78
7.1.320.0010.01415.94
7.1.310.0050.00715.90
7.1.300.0050.01015.85
7.1.290.0040.01015.90
7.1.280.0100.00515.85
7.1.270.0040.00815.76
7.1.260.0060.00815.88
7.1.250.0050.00815.86
7.1.240.0110.00415.78
7.1.230.0040.00415.78
7.1.220.0060.00815.64
7.1.210.0090.00615.96
7.1.200.0020.01215.81
7.1.190.0070.00415.69
7.1.180.0030.01015.79
7.1.170.0560.00314.98
7.1.160.0440.00816.33
7.1.150.0500.01016.52
7.1.140.0500.01116.28
7.1.130.0440.01016.42
7.1.120.0420.00716.23
7.1.110.0510.00816.09
7.1.100.0650.00615.90
7.1.90.0670.00815.68
7.1.80.0630.00916.12
7.1.70.0640.00815.55
7.1.60.0520.01024.65
7.1.50.0760.00924.54
7.1.40.0520.01424.27
7.1.30.0510.01424.22
7.1.20.0950.00724.24
7.1.10.0520.01315.32
7.1.00.0560.00615.31
7.0.330.0100.00315.18
7.0.320.0000.01115.42
7.0.310.0060.00315.48
7.0.300.0040.01215.42
7.0.290.0120.00315.19
7.0.280.0030.01015.51
7.0.270.0080.00415.42
7.0.260.0050.00515.57
7.0.250.0000.01515.50
7.0.240.0000.01015.31
7.0.230.0040.01115.62
7.0.220.0040.01115.46
7.0.210.0070.01115.59
7.0.200.0030.01015.57
7.0.190.0000.01515.59
7.0.180.0000.00915.52
7.0.170.0130.00315.53
7.0.160.0110.00415.52
7.0.150.0060.00915.68
7.0.140.0060.00615.51
7.0.130.0000.01315.25
7.0.120.0100.00715.66
7.0.110.0030.01015.24
7.0.100.0120.00315.20
7.0.90.0060.00615.39
7.0.80.0000.01115.28
7.0.70.0060.00915.13
7.0.60.0030.01015.52
7.0.50.0000.01515.50
7.0.40.0070.00413.49
7.0.30.0060.00313.39
7.0.20.0040.01413.55
7.0.10.0080.00813.58
7.0.00.0040.01113.51
5.6.400.0030.01414.19
5.6.390.0000.01314.03
5.6.380.0060.00914.32
5.6.370.0070.00414.35
5.6.360.0090.00914.11
5.6.350.0030.01414.39
5.6.340.0030.01014.22
5.6.330.0070.00713.96
5.6.320.0110.00314.01
5.6.310.0040.01114.34
5.6.300.0060.00614.10
5.6.290.0070.00714.11
5.6.280.0000.01314.11
5.6.270.0000.01613.89
5.6.260.0090.00614.35
5.6.250.0070.01014.04
5.6.240.0060.00314.27
5.6.230.0090.00314.36
5.6.220.0000.01414.15
5.6.210.0030.00914.04
5.6.200.0070.01014.22
5.6.190.0090.00614.13
5.6.180.0090.00014.38
5.6.170.0080.00314.32
5.6.160.0110.00413.95
5.6.150.0090.00613.99
5.6.140.0100.00014.09
5.6.130.0040.00413.91
5.6.120.0070.01014.15
5.6.110.0060.01014.03
5.6.100.0040.01113.93
5.6.90.0100.00314.21
5.6.80.0090.00514.20
5.6.70.0080.00814.20
5.6.60.0130.00313.95
5.6.50.0040.01213.93
5.6.40.0080.00513.95
5.6.30.0100.00314.02
5.6.20.0040.01413.98
5.6.10.0150.00014.21
5.6.00.0000.01714.03

preferences:
59.72 ms | 400 KiB | 5 Q