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.0150.00618.68
8.3.50.0070.01116.25
8.3.40.0090.00618.63
8.3.30.0110.00418.84
8.3.20.0070.00018.98
8.3.10.0040.00420.73
8.3.00.0000.00817.80
8.2.180.0190.00016.75
8.2.170.0060.00922.96
8.2.160.0070.00720.77
8.2.150.0000.00824.18
8.2.140.0040.00424.66
8.2.130.0000.00819.70
8.2.120.0000.00826.35
8.2.110.0040.00422.01
8.2.100.0080.00319.51
8.2.90.0080.00017.63
8.2.80.0000.00918.61
8.2.70.0040.00417.52
8.2.60.0000.00917.80
8.2.50.0000.00918.10
8.2.40.0040.00420.95
8.2.30.0070.00019.19
8.2.20.0050.00317.97
8.2.10.0040.00417.90
8.2.00.0040.00419.25
8.1.280.0070.00725.92
8.1.270.0060.00323.95
8.1.260.0040.00426.35
8.1.250.0040.00428.09
8.1.240.0050.00524.01
8.1.230.0060.00619.05
8.1.220.0080.00017.74
8.1.210.0080.00018.77
8.1.200.0060.00317.10
8.1.190.0050.00317.10
8.1.180.0000.00718.10
8.1.170.0080.00018.50
8.1.160.0070.00018.79
8.1.150.0000.00718.67
8.1.140.0040.00417.54
8.1.130.0070.00018.92
8.1.120.0040.00617.32
8.1.110.0050.00217.19
8.1.100.0050.00317.25
8.1.90.0000.00817.32
8.1.80.0000.00817.20
8.1.70.0030.00317.33
8.1.60.0030.00517.48
8.1.50.0000.00817.35
8.1.40.0040.00417.36
8.1.30.0060.00617.54
8.1.20.0000.00817.45
8.1.10.0000.00817.32
8.1.00.0040.00417.21
8.0.300.0050.00318.77
8.0.290.0070.00016.63
8.0.280.0030.00318.30
8.0.270.0030.00317.05
8.0.260.0000.00718.98
8.0.250.0040.00416.92
8.0.240.0040.00416.84
8.0.230.0030.00516.86
8.0.220.0000.00716.86
8.0.210.0000.00716.75
8.0.200.0000.00716.81
8.0.190.0000.00816.98
8.0.180.0040.00416.91
8.0.170.0030.00616.89
8.0.160.0040.00416.94
8.0.150.0030.00316.64
8.0.140.0050.00316.85
8.0.130.0000.00513.41
8.0.120.0000.00816.84
8.0.110.0040.00416.73
8.0.100.0040.00416.81
8.0.90.0040.00416.70
8.0.80.0080.01416.84
8.0.70.0040.00416.72
8.0.60.0050.00216.83
8.0.50.0050.00216.76
8.0.30.0100.01016.92
8.0.20.0120.00717.40
8.0.10.0050.00216.73
8.0.00.0100.01016.73
7.4.330.0000.00615.55
7.4.320.0000.00616.69
7.4.300.0050.00316.65
7.4.290.0030.00316.60
7.4.280.0040.00416.45
7.4.270.0100.00016.48
7.4.260.0000.00816.67
7.4.250.0040.00416.50
7.4.240.0050.00316.50
7.4.230.0000.00816.63
7.4.220.0030.00516.41
7.4.210.0140.00416.72
7.4.200.0080.00016.47
7.4.160.0070.00916.52
7.4.140.0150.01017.86
7.4.130.0060.01216.64
7.4.120.0080.01316.59
7.4.110.0090.00916.61
7.4.100.0030.01416.52
7.4.90.0070.01116.72
7.4.80.0060.01319.39
7.4.70.0090.00916.46
7.4.60.0060.01216.64
7.4.50.0000.01516.60
7.4.40.0090.00916.63
7.4.00.0030.01015.01
7.3.330.0000.00613.49
7.3.320.0030.00313.47
7.3.310.0030.00316.58
7.3.300.0030.00316.38
7.3.290.0040.00416.38
7.3.280.0080.01316.48
7.3.260.0090.00916.47
7.3.240.0100.00716.64
7.3.230.0110.00616.66
7.3.210.0050.01816.44
7.3.200.0150.00316.75
7.3.190.0080.00816.63
7.3.180.0070.01416.50
7.3.170.0110.01716.47
7.3.160.0100.00716.48
7.3.10.0180.00716.74
7.3.00.0210.00916.68
7.2.330.0050.01316.86
7.2.320.0090.00816.74
7.2.310.0090.00916.56
7.2.300.0070.01116.57
7.2.290.0090.01316.93
7.2.130.0540.01216.83
7.2.120.0150.01216.89
7.2.110.0110.01116.81
7.2.100.0170.00616.28
7.2.90.0170.01016.69
7.2.80.0200.00616.56
7.2.70.0220.00816.73
7.2.60.0610.01716.06
7.2.50.0630.00915.78
7.2.40.0610.00516.03
7.2.30.0550.01116.06
7.2.20.0560.00816.04
7.2.10.0660.00816.04
7.2.00.0710.00516.16
7.1.250.0220.01015.75
7.1.200.0030.00915.78
7.1.170.1060.01214.03
7.1.160.1130.01116.60
7.1.150.1100.01416.90
7.1.140.1470.01216.89
7.1.130.1050.01216.66
7.1.120.1410.01316.45
7.1.110.1300.00816.18
7.1.100.1350.01216.00
7.1.90.1000.01116.04
7.1.80.0950.00916.05
7.1.70.1350.01215.08
7.1.60.1100.01533.35
7.1.50.1230.01233.09
7.1.40.1320.01432.89
7.1.30.1260.02032.90
7.1.20.1650.01132.58
7.1.10.1120.01014.73
7.1.00.1460.01414.81

preferences:
65.89 ms | 400 KiB | 5 Q