Skip to content

Instantly share code, notes, and snippets.

@DenesKellner
Last active September 12, 2021 22:51
Show Gist options
  • Select an option

  • Save DenesKellner/32397e3d8cae23e45de3b681851fbfee to your computer and use it in GitHub Desktop.

Select an option

Save DenesKellner/32397e3d8cae23e45de3b681851fbfee to your computer and use it in GitHub Desktop.
A minimal benchmark framework that gives you high precision time measurements. The higher you set $outerLoops the more precise results you get.
<?php
'Options';{
$outerLoops = 1000; // number of times to repeat measure (choose shortest time), give it 1000+
$innerLoops = 30; // iterations within each measurement; depends on what you're measuring
$magnitude = 6; // one of [3,6,9], or [nanoseconds,microseconds,milliseconds], respectively
$decimals = 2; // normally 2 but be my guest
$methods = 3; // number (and order) of methods to run; simple integer or like [1,3,2]
}
'Test variables';{
$testString = "
Professional dreamers are highly paid, respected, much sought-after talents. Like the majority of
us, these seven dreamt without effort or discipline. Dreaming professionally, so that one’s dreams
can be recorded and played back for the entertainment of others, is a much more demanding
proposition. It requires the ability to regulate semiconscious creative impulses and to stratify
imagination, an extraordinarily difficult combination to achieve. A professional dreamer is
simultaneously the most organized of all artists and the most spontaneous. A subtle weaver of
speculation, not straightforward and clumsy like you or I. Or these certain seven sleepers.
";
$testString = str_repeat($testString,5);
$replacements = [
"respected" => "despised",
"seven" => "ninety-five",
"difficult" => "boring",
"professional" => "confused",
"subtle" => "sigourney",
];
$replace1 = array_keys($replacements);
$replace2 = array_keys($replacements);
}
?><!doctype html>
<html>
<title> SpeedTest </title>
<style>
* {box-sizing:border-box;}
body {background:black;padding:20px 30px;}
pre {color:#ae0;font-size:14px;}
pre b {font-weight:normal;color:white;}
</style>
<body>
<pre><?php
function ms() {return (double)1000*microtime(1);}
if(is_integer($magnitude)) $magnitude = [3=>"milli",6=>"micro",9=>"nano"][$magnitude]."seconds";
print "Ticks are $magnitude.\n\n";
$methodNumbers = [];
if( is_array($methods)) $methodNumbers = $methods;
if(!is_array($methods)) $methodNumbers = range(1,$methods);
foreach($methodNumbers as $method) {
$m = 9999999;
for($i=0;$i<$outerLoops;++$i) {
$t = ms();
$N = $innerLoops;
'Run current method';{
if($method==1) while($N-->0) {
;;
;;
;; $tmp = strtr($testString,$replacements);
;;
;;
}
if($method==2) while($N-->0) {
;;
;;
;; $tmp = str_replace($testString,$replace1,$replace2);
;;
;;
}
if($method==3) while($N-->0) {
;;
;;
;;
;;
;;
}
}
$m = min(ms()-$t,$m);
}
switch($magnitude) {
case 'nanoseconds': {$div=1000000;break;}
case 'microseconds': {$div=1000;break;}
case 'milliseconds': {$div=1;break;}
}
printf("Method $method's best time: <b>%11.{$decimals}f</b> ticks\n",$m/($innerLoops/$div));
}
?></pre>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment