- array_sum: documentation ( source)
- microtime: documentation ( source)
- array_shift: documentation ( source)
- array_pop: documentation ( source)
- printf: documentation ( source)
<?php
// Instantiate object as singleton with private constructor
class Test1 {
protected static $instance;
private function __construct() {}
public static function getInstance() {
return isset(static::$instance) ? static::$instance : static::$instance = new static;
}
}
// Instantiate object from caller
class Test2 {
public function __construct() {}
}
for ($t = 0; $t < 10000; $t++) {
$s = microtime(true);
$obj = Test1::getInstance();
$s = microtime(true) - $s;
$tests['Test1'][] = $s;
}
for ($t = 0; $t < 10000; $t++) {
$s = microtime(true);
$obj = new Test2;
$s = microtime(true) - $s;
$tests['Test2'][] = $s;
}
printf("Test1 first result: %.9f s\n", array_shift($tests['Test1']));
printf("Test1 last result: %.9f s\n", array_pop($tests['Test1']));
printf("Test2 first result: %.9f s\n", array_shift($tests['Test2']));
printf("Test2 last result: %.9f s\n", array_pop($tests['Test2']));
printf("\nTest1 avg result: %.9f s\n", array_sum($tests['Test1']) / count($tests['Test1']));
printf("\nTest2 avg result: %.9f s\n", array_sum($tests['Test2']) / count($tests['Test2']));