<?php
function timeit(callable $func, array $args, int $times = 50000)
{
if ($times < 1) {
throw new InvalidArgumentException('times must be greater than 1');
}
$total = 0;
for ($i = 0; $i < $times; $i++) {
$initial = microtime(true);
try {
$func(...$args);
} catch (Exception $e) {
// nothing to do here.
}
$total += microtime(true) - $initial;
}
$per_call = $total/$times;
echo "$times calls took $total seconds ($per_call seconds per call)\n";
}
timeit('explode', [',', 'cat,dog,horse']);
To embed this program on your website, copy the following code and paste it into your website's HTML: