3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * @template TKey of array-key * @template TValue */ class Collection { /** * @var array<TKey, TValue> */ protected $items = []; /** * @param array<TKey, TValue> $items * @return void */ public function __construct($items = []) { $this->items = $items; } /** * @param TValue|array-key $key * @return bool */ public function containsStrict_foreach($key) { foreach ($this->items as $item) { if ($item === $key) { return true; } } return false; } /** * @param TValue|array-key $key * @return bool */ public function containsStrict_in_array($key) { return in_array($key, $this->items, true); } } /** * @template TValue * @param array<array-key, TValue> $items * @return array{int, int} */ function bench($items): array { $value = $items[count($items) / 2]; $collection = new Collection($items); $prev = hrtime(true); $collection->containsStrict_foreach($value); $foreach = hrtime(true) - $prev; $collection = new Collection($items); $prev = hrtime(true); $collection->containsStrict_in_array($value); $inArray = hrtime(true) - $prev; return [$foreach, $inArray]; } $fmt = "%5s | %-13.13s | %24s | %25s\n"; printf($fmt, 'n', 'name', 'containsStrict (foreach)', 'containsStrict (in_array)'); foreach ([100, 1000, 10000] as $n) { printf($fmt, $n, "serial int", ...bench(range(1, $n))); printf($fmt, $n, "random int", ...bench(array_map(fn () => random_int(1, $n), range(1, $n)))); printf($fmt, $n, "uniqid string", ...bench(array_map(fn () => uniqid(), range(1, $n)))); }

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.1.120.0160.02022.61
8.1.110.0150.02321.82
8.1.100.0220.01522.40
8.1.90.0160.02321.89
8.1.80.0210.01523.66
8.1.70.0150.02321.86
8.1.60.0240.01523.84
8.1.50.0140.03121.95
8.1.40.0140.02523.86
8.1.30.0230.01722.03
8.1.20.0190.01922.02
8.1.10.0140.02523.70
8.1.00.0220.01821.85
8.0.250.0090.01821.86
8.0.240.0120.02321.14
8.0.230.0120.02421.14
8.0.220.0270.01020.98
8.0.210.0140.02021.06
8.0.200.0170.01721.07
8.0.190.0120.02523.06
8.0.180.0150.02221.13
8.0.170.0130.02321.14
8.0.160.0200.01721.13
8.0.150.0170.01821.00
8.0.140.0140.02320.96
8.0.130.0250.01121.08
8.0.120.0190.01721.13
8.0.110.0180.01821.04
8.0.100.0220.01623.05
8.0.90.0200.01620.95
8.0.80.0160.02021.90
8.0.70.0200.01421.02
8.0.60.0210.01521.77
8.0.50.0150.01921.02
8.0.30.0090.02621.02
8.0.20.0150.01521.81
8.0.10.0210.01221.08
7.4.330.0140.01616.91
7.4.320.0180.01820.70
7.4.300.0220.01220.63
7.4.290.0030.02723.35
7.4.280.0040.02620.63
7.4.270.0080.02420.28
7.4.260.0100.01921.08
7.4.250.0090.02320.71
7.4.240.0160.01621.34
7.4.230.0220.01320.73
7.4.220.0160.01620.63
7.4.210.0130.02120.61
7.4.200.0130.02020.73
7.4.190.0180.01220.86
7.4.180.0090.02121.31
7.4.160.0100.01920.86
7.4.150.0140.01721.29
7.4.140.0170.01420.54
7.4.130.0190.01420.78
7.4.120.0080.02020.34
7.4.110.0140.01820.39
7.4.100.0070.02121.32
7.4.90.0130.01920.52
7.4.80.0090.02220.41
7.4.70.0150.01821.44
7.4.60.0140.01520.66
7.4.50.0070.02720.36
7.4.40.0160.01320.53
7.4.30.0190.01320.61
7.4.20.0130.01920.70
7.4.10.0100.02021.38
7.4.00.0070.02223.11
7.3.00.0110.00517.33
7.2.00.0090.00817.33
7.1.00.0080.00417.33
7.0.00.0090.00317.33
5.4.450.0050.00317.33
5.3.290.0040.00517.33
5.0.00.0010.00317.33
4.3.00.0010.00117.33

preferences:
69.62 ms | 403 KiB | 5 Q