Skip to content

Instantly share code, notes, and snippets.

@vtr0n
Last active July 13, 2018 08:22
Show Gist options
  • Save vtr0n/441a75a33bb8cafbc3bd04eeada0e6ce to your computer and use it in GitHub Desktop.
Save vtr0n/441a75a33bb8cafbc3bd04eeada0e6ce to your computer and use it in GitHub Desktop.
PHP benchmark static function, object function, function
<?php
class SomeStaticClass
{
public static function doTest($i) {
sqrt($i);
}
}
class SomeClass
{
public function doTest($i) {
sqrt($i);
}
}
function someTest($i){
sqrt($i);
}
// bench static method
$startTime = microtime(true);
for ($i = 0; $i< 10*1000*1000; $i++)
SomeStaticClass::doTest($i);
echo "Static Time: " , (microtime(true)-$startTime) , " ms\n";
// bench object method
$startTime = microtime(true);
$someObj = new SomeClass();
for ($i = 0; $i< 10*1000*1000; $i++)
$someObj->doTest($i);
echo "Object Time: " , (microtime(true)-$startTime) , " ms\n";
// bench function
$startTime = microtime(true);
for ($i = 0; $i< 10*1000*1000; $i++)
someTest($i);
echo "Function Time: " , (microtime(true)-$startTime) , " ms\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment