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)))); }
Output for git.master
n | name | containsStrict (foreach) | containsStrict (in_array) 100 | serial int | 2965 | 911 100 | random int | 461 | 330 100 | uniqid string | 1282 | 751 1000 | serial int | 5521 | 661 1000 | random int | 5490 | 721 1000 | uniqid string | 7264 | 3587 10000 | serial int | 58930 | 4979 10000 | random int | 43652 | 4920 10000 | uniqid string | 89820 | 45165
Output for git.master_jit
n | name | containsStrict (foreach) | containsStrict (in_array) 100 | serial int | 2705 | 1082 100 | random int | 782 | 291 100 | uniqid string | 1102 | 711 1000 | serial int | 5450 | 601 1000 | random int | 5821 | 691 1000 | uniqid string | 7504 | 4188 10000 | serial int | 57959 | 7855 10000 | random int | 51156 | 5040 10000 | uniqid string | 120507 | 51997

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
66.31 ms | 409 KiB | 5 Q