3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace Bench; /** * 1. ZEND_IS_BOOL 命令にコンパイルされる (最速期待) */ function is_bool_native(mixed $v): bool { return \is_bool($v); } /** * 2. 名前空間解決による DO_FCALL。実行時にグローバル関数を探す */ function is_bool_ns(mixed $v): bool { return is_bool($v); } /** * 3. ZEND_IN_ARRAY 命令にコンパイルされる (O(1) 最適化) */ function in_array_native(mixed $v): bool { return \in_array($v, [true, false], true); } /** * 4. 名前空間解決 + DO_FCALL + 通常の線形探索 (O(n)) */ function in_array_ns(mixed $v): bool { return in_array($v, [true, false], true); } /** * 5. ユーザー定義ロジック:キャストと比較 */ function is_bool_cast(mixed $v): bool { return $v === (bool)$v; } /** * 6. ユーザー定義ロジック:論理和 */ function is_bool_or(mixed $v): bool { return $v === true || $v === false; } /** * 7. match式 */ function is_bool_match(mixed $v): bool { return match ($v) { true, false => true, default => false, }; } /** * データセット */ $datasets = [ 'all_bool' => [true, false, true, false], 'no_bool' => [1, 0, "1", "0", "true", [], new \stdClass(), null], 'half_half' => [true, 1, false, "test", true, null, false, []], ]; $testcases = [ 'is_bool (\native)' => is_bool_native(...), 'is_bool (ns)' => is_bool_ns(...), 'in_array (\native)' => in_array_native(...), 'in_array (ns)' => in_array_ns(...), 'is_bool_cast' => is_bool_cast(...), 'logical OR' => is_bool_or(...), 'match_expression' => is_bool_match(...), ]; $iterations = 100000; echo "PHP Version: " . PHP_VERSION . "\n"; echo str_repeat('=', 60) . "\n"; foreach ($datasets as $data_name => $values) { echo "\nDataset: $data_name\n"; echo str_repeat('-', 60) . "\n"; $results = []; foreach ($testcases as $name => $test) { $start = hrtime(true); for ($i = 0; $i < $iterations; $i++) { foreach ($values as $value) { $test($value); } } $end = hrtime(true); // 合計実行回数で割って平均を出すのではなく、あえて「このデータセット一式の処理時間」とする $results[$name] = ($end - $start) / 1e+6; } asort($results); foreach ($results as $name => $ms) { printf("%-20s : %10.4f ms\n", $name, $ms); } }

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.5.20.0080.41018.07
8.5.10.0260.41816.67
8.5.00.0100.37916.51
8.4.170.0180.41520.67
8.4.160.0130.39520.55
8.4.150.0120.37819.65
8.4.140.0120.39517.72
8.4.130.0200.41717.39
8.4.120.0160.40317.75
8.4.110.0200.42517.86
8.4.100.0240.38717.85
8.4.90.0330.41217.66
8.4.80.0170.38217.93
8.4.70.0170.36317.99
8.4.60.0200.40017.91
8.4.50.0380.40617.95
8.4.40.0270.36617.59
8.4.30.0180.36517.60
8.4.20.0270.41217.36
8.4.10.0240.43717.68
8.3.300.0120.43620.91
8.3.290.0250.38918.27
8.3.280.0150.42818.18
8.3.270.0140.39616.81
8.3.260.0300.43616.82
8.3.250.0320.42516.66
8.3.240.0200.40716.68
8.3.230.0150.40916.86
8.3.220.0350.43616.86
8.3.210.0250.45216.28
8.3.200.0370.46516.29
8.3.190.0250.43716.41
8.3.180.0060.47116.90
8.3.170.0140.47616.77
8.3.160.0090.47216.75
8.3.150.0050.39916.79
8.3.140.0110.44216.71
8.3.130.0050.39516.79
8.3.120.0160.43616.70
8.3.110.0030.40716.70
8.3.100.0060.38316.79
8.3.90.0080.39116.92
8.3.80.0130.44116.87
8.3.70.0120.43816.80
8.3.60.0100.43716.67
8.3.50.0070.38616.86
8.3.40.0100.39517.96
8.3.30.0100.45317.98
8.3.20.0130.46017.80
8.3.10.0150.44218.04
8.3.00.0140.44418.04

preferences:
29.34 ms | 403 KiB | 5 Q