Skip to content

Instantly share code, notes, and snippets.

@marek-saji
Last active December 30, 2015 03:49
Show Gist options
  • Save marek-saji/7772077 to your computer and use it in GitHub Desktop.
Save marek-saji/7772077 to your computer and use it in GitHub Desktop.
Rubbish benchmark of design patterns for grouping functions together
<?php
/**
* Performance of design patterns for grouping functions together
*
* ^C, when you get bored.
*/
error_reporting(E_ALL);
class SomeClass
{
static public function doTest($var) {return $var;}
}
class SomeObj
{
public function doTest($var) {return $var;}
static public function doTest2($var) {return $var;}
}
class SomeSingleton
{
static private $_instance = null;
private function __construct () {}
static public function getInstance () {
if (null === self::$_instance) self::$_instance = new self;
return self::$_instance;
}
public function doTest($var) {return $var;}
}
// if you don't trust me that there is no performance penalty,
// put it in separate file and include it
eval('namespace SomeNameSpace; function doTest($var) { return $var; }');
// show result of a test
function result ($title, $starttime)
{
global $base;
$time = microtime(true) - $starttime;
if (null === $base) $base = $time;
printf("%-24s %0.16f %f\n", $title, $time, $time / $base);
}
// run each test $count times
function test ($count)
{
global $base;
$base = null;
echo PHP_EOL, '# count=', $count, PHP_EOL;
$starttime = microtime(true);
for ($i = 0; $i< $count; $i++)
{
SomeClass::doTest($i);
SomeClass::doTest($i);
SomeClass::doTest($i);
SomeClass::doTest($i);
SomeClass::doTest($i);
SomeClass::doTest($i);
SomeClass::doTest($i);
SomeClass::doTest($i);
SomeClass::doTest($i);
SomeClass::doTest($i);
}
result("Static call", $starttime);
$starttime = microtime(true);
$obj = new SomeObj();
for ($i = 0; $i< $count; $i++)
{
$obj->doTest($i);
$obj->doTest($i);
$obj->doTest($i);
$obj->doTest($i);
$obj->doTest($i);
$obj->doTest($i);
$obj->doTest($i);
$obj->doTest($i);
$obj->doTest($i);
$obj->doTest($i);
}
result("Object method", $starttime);
$starttime = microtime(true);
for ($i = 0; $i< $count; $i++)
{
SomeObj::doTest2($i);
SomeObj::doTest2($i);
SomeObj::doTest2($i);
SomeObj::doTest2($i);
SomeObj::doTest2($i);
SomeObj::doTest2($i);
SomeObj::doTest2($i);
SomeObj::doTest2($i);
SomeObj::doTest2($i);
SomeObj::doTest2($i);
}
result("Static call #2", $starttime);
$starttime = microtime(true);
for ($i = 0; $i< $count; $i++)
{
SomeSingleton::getInstance()->doTest($i);
SomeSingleton::getInstance()->doTest($i);
SomeSingleton::getInstance()->doTest($i);
SomeSingleton::getInstance()->doTest($i);
SomeSingleton::getInstance()->doTest($i);
SomeSingleton::getInstance()->doTest($i);
SomeSingleton::getInstance()->doTest($i);
SomeSingleton::getInstance()->doTest($i);
SomeSingleton::getInstance()->doTest($i);
SomeSingleton::getInstance()->doTest($i);
}
result("Singleton", $starttime);
$starttime = microtime(true);
for ($i = 0; $i< $count; $i++)
{
SomeNameSpace\doTest($i);
SomeNameSpace\doTest($i);
SomeNameSpace\doTest($i);
SomeNameSpace\doTest($i);
SomeNameSpace\doTest($i);
SomeNameSpace\doTest($i);
SomeNameSpace\doTest($i);
SomeNameSpace\doTest($i);
SomeNameSpace\doTest($i);
SomeNameSpace\doTest($i);
}
result("Namespaced function", $starttime);
}
echo PHP_VERSION, PHP_EOL;
for ($count = 1 ;; $count *= 10) test($count);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment