@ 2020-01-10T18:53:27Z <?php
set_time_limit(0);
Class Static0 {
public static $counter = 0;
public static function incrByTwo()
{
self::$counter += 2;
}
}
Class Static1 extends Static0 {
public static function incrByOne()
{
self::$counter++;
}
public static function incrByTwo()
{
parent::incrByTwo();
}
}
Class Dynamic0 {
public $counter = 0;
public function incrByTwo()
{
$this->counter += 2;
}
}
Class Dynamic1 extends Dynamic0 {
public function incrByOne()
{
$this->counter++;
}
public function incrByTwo()
{
parent::incrByTwo();
}
}
Class Singleton0 {
private static $instances = [];
public static function getInstance()
{
$self = static::class;
if (!isset(self::$instances[$self])) {
self::$instances[$self] = new $self;
}
return self::$instances[$self];
}
final private function __construct()
{
}
final private function __clone()
{
}
protected function __wakeup()
{
}
public $counter = 0;
public function incrByTwo()
{
$this->counter += 2;
}
}
Class Singleton1 extends Singleton0 {
public function incrByOne()
{
$this->counter++;
}
public function incrByTwo()
{
parent::incrByTwo();
}
}
function incrByOne_by_ref(&$counter)
{
$counter++;
}
function parent_incrByTwo_by_ref(&$counter)
{
$counter += 2;
}
function incrByTwo_by_ref(&$counter)
{
parent_incrByTwo_by_ref($counter);
}
function incrByOne_global()
{
global $counter;
$counter++;
}
function parent_incrByTwo_global()
{
global $counter;
$counter += 2;
}
function incrByTwo_global()
{
parent_incrByTwo_global();
}
function incrByOne_globals()
{
$GLOBALS['counter']++;
}
function parent_incrByTwo_globals()
{
$GLOBALS['counter'] += 2;
}
function incrByTwo_globals()
{
parent_incrByTwo_globals();
}
abstract class MyStorage {
public static $counter = 0;
}
function incrByOne_static_prop()
{
MyStorage::$counter++;
}
function parent_incrByTwo_static_prop()
{
MyStorage::$counter += 2;
}
function incrByTwo_static_prop()
{
parent_incrByTwo_static_prop();
}
// free runs so everybody is well loaded
$runs=10;
for($i=0;$i<$runs;$i++) {
$obj = new Dynamic1();
$obj->incrByOne();
$obj->incrByTwo();
}
for($i=0;$i<$runs;$i++) {
Static1::incrByOne();
Static1::incrByTwo();
}
unset($obj, $i);
$runs=500000;
$memories = [memory_get_usage()];
?>
Runs by case: <?= $runs ?>
Memory usage at start: <?= number_format(end($memories)) ?>
<?php
$time_start = microtime(true);
$obj = new Dynamic1();
for($i=0;$i<$runs;$i++) {
$obj->incrByOne();
$obj->incrByTwo();
}
$time_end = microtime(true);
$time1 = $time_end - $time_start;
$last_memory = end($memories);
$memories[] = memory_get_usage();
$memory_diff = end($memories) - $last_memory;
?>
+ Dynamic
Total execution time is <?= number_format($time1, 4) ?>
Used memory: <?= number_format($memory_diff) ?>
<?php
$time_start = microtime(true);
for($i=0;$i<$runs;$i++) {
$obj = new Dynamic1();
$obj->incrByOne();
$obj->incrByTwo();
}
$time_end = microtime(true);
$time1 = $time_end - $time_start;
$last_memory = end($memories);
$memories[] = memory_get_usage();
$memory_diff = end($memories) - $last_memory;
?>
+ Dynamic instantiated
Total execution time is <?= number_format($time1, 4) ?>
Used memory: <?= number_format($memory_diff) ?>
<?php
$storage = [];
$time_start = microtime(true);
for($i=0;$i<$runs;$i++) {
$obj = new Dynamic1();
$obj->incrByOne();
$obj->incrByTwo();
$storage[] = $obj;
}
$time_end = microtime(true);
$time1 = $time_end - $time_start;
$last_memory = end($memories);
$memories[] = memory_get_usage();
$memory_diff = end($memories) - $last_memory;
unset($storage);
$memories[] = memory_get_usage();
?>
+ Dynamic instantiated stored
Total execution time is <?= number_format($time1, 4) ?>
Used memory: <?= number_format($memory_diff) ?>
<?php
$storage2 = [];
$obj = new Dynamic1();
$time_start = microtime(true);
for($i=0;$i<$runs;$i++) {
$storage2[] = $obj;
}
$time_end = microtime(true);
$time1 = $time_end - $time_start;
$last_memory = end($memories);
$memories[] = memory_get_usage();
$memory_diff = end($memories) - $last_memory;
unset($storage2);
$memories[] = memory_get_usage();
?>
+ Storage only
Total execution time is <?= number_format($time1, 4) ?>
Used memory: <?= number_format($memory_diff) ?>
<?php
$time_start = microtime(true);
for($i=0;$i<$runs;$i++) {
Static1::incrByOne();
Static1::incrByTwo();
}
$time_end = microtime(true);
$time1 = $time_end - $time_start;
$last_memory = end($memories);
$memories[] = memory_get_usage();
$memory_diff = end($memories) - $last_memory;
?>
+ Static
Total execution time is <?= number_format($time1, 4) ?>
Used memory: <?= number_format($memory_diff) ?>
<?php
$time_start = microtime(true);
for($i=0;$i<$runs;$i++) {
$obj = Singleton1::getInstance();
$obj->incrByOne();
$obj->incrByTwo();
}
$time_end = microtime(true);
$time1 = $time_end - $time_start;
$last_memory = end($memories);
$memories[] = memory_get_usage();
$memory_diff = end($memories) - $last_memory;
?>
+ Singletons with many getInstance
Total execution time is <?= number_format($time1, 4) ?>
Used memory: <?= number_format($memory_diff) ?>
<?php
$time_start = microtime(true);
$obj = Singleton1::getInstance();
for($i=0;$i<$runs;$i++) {
$obj->incrByOne();
$obj->incrByTwo();
}
$time_end = microtime(true);
$time1 = $time_end - $time_start;
$last_memory = end($memories);
$memories[] = memory_get_usage();
$memory_diff = end($memories) - $last_memory;
?>
+ Singletons with one getInstance
Total execution time is <?= number_format($time1, 4) ?>
Used memory: <?= number_format($memory_diff) ?>
<?php
$counter = 0;
$time_start = microtime(true);
for($i=0;$i<$runs;$i++) {
incrByOne_globals();
incrByTwo_globals();
}
$time_end = microtime(true);
$time1 = $time_end - $time_start;
$last_memory = end($memories);
$memories[] = memory_get_usage();
$memory_diff = end($memories) - $last_memory;
?>
+ Functions with $GLOBALS
Total execution time is <?= number_format($time1, 4) ?>
Used memory: <?= number_format($memory_diff) ?>
<?php
$counter = 0;
$time_start = microtime(true);
for($i=0;$i<$runs;$i++) {
incrByOne_global();
incrByTwo_global();
}
$time_end = microtime(true);
$time1 = $time_end - $time_start;
$last_memory = end($memories);
$memories[] = memory_get_usage();
$memory_diff = end($memories) - $last_memory;
?>
+ Functions with global
Total execution time is <?= number_format($time1, 4) ?>
Used memory: <?= number_format($memory_diff) ?>
<?php
$counter = 0;
$time_start = microtime(true);
for($i=0;$i<$runs;$i++) {
incrByOne_by_ref($counter);
incrByTwo_by_ref($counter);
}
$time_end = microtime(true);
$time1 = $time_end - $time_start;
$last_memory = end($memories);
$memories[] = memory_get_usage();
$memory_diff = end($memories) - $last_memory;
?>
+ Functions with $counter passed by ref
Total execution time is <?= number_format($time1, 4) ?>
Used memory: <?= number_format($memory_diff) ?>
<?php
MyStorage::$counter = 0;
$time_start = microtime(true);
for($i=0;$i<$runs;$i++) {
incrByOne_static_prop($counter);
incrByTwo_static_prop($counter);
}
$time_end = microtime(true);
$time1 = $time_end - $time_start;
$last_memory = end($memories);
$memories[] = memory_get_usage();
$memory_diff = end($memories) - $last_memory;
?>
+ Functions with $counter stored in a static property
Total execution time is <?= number_format($time1, 4) ?>
Used memory: <?= number_format($memory_diff) ?>
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 8.3.7 0.052 0.339 56.60 8.3.6 0.059 0.332 56.48 8.3.5 0.040 0.359 56.41 8.3.4 0.034 0.101 40.16 8.3.3 0.030 0.112 40.17 8.3.2 0.032 0.100 40.20 8.3.1 0.037 0.093 40.04 8.3.0 0.026 0.105 40.33 8.2.19 0.046 0.342 56.62 8.2.18 0.058 0.340 56.53 8.2.17 0.033 0.092 40.68 8.2.16 0.041 0.091 40.77 8.2.15 0.031 0.101 40.60 8.2.14 0.027 0.103 40.63 8.2.13 0.033 0.096 40.58 8.2.12 0.035 0.097 40.16 8.2.11 0.034 0.104 40.06 8.2.10 0.027 0.108 40.44 8.2.9 0.024 0.107 40.47 8.2.8 0.028 0.103 40.45 8.2.7 0.038 0.092 40.46 8.2.6 0.033 0.100 40.45 8.2.5 0.039 0.116 40.54 8.2.4 0.030 0.105 40.50 8.2.3 0.045 0.083 40.32 8.2.2 0.030 0.096 40.27 8.2.1 0.027 0.099 40.40 8.2.0 0.044 0.098 40.26 8.1.28 0.067 0.391 64.20 8.1.27 0.050 0.091 44.00 8.1.26 0.037 0.103 44.14 8.1.25 0.033 0.107 44.51 8.1.24 0.037 0.093 44.34 8.1.23 0.034 0.112 44.48 8.1.22 0.021 0.110 44.29 8.1.21 0.030 0.104 44.17 8.1.20 0.044 0.092 44.27 8.1.19 0.057 0.101 43.84 8.1.18 0.054 0.085 43.94 8.1.17 0.044 0.116 43.96 8.1.16 0.046 0.106 43.95 8.1.15 0.045 0.096 44.50 8.1.14 0.037 0.112 44.30 8.1.13 0.032 0.107 44.37 8.1.12 0.041 0.102 44.38 8.1.11 0.024 0.124 44.21 8.1.10 0.027 0.105 44.38 8.1.9 0.037 0.096 44.12 8.1.8 0.046 0.111 44.30 8.1.7 0.044 0.111 44.25 8.1.6 0.048 0.092 44.25 8.1.5 0.050 0.103 44.19 8.1.4 0.040 0.099 43.96 8.1.3 0.037 0.110 44.18 8.1.2 0.049 0.092 44.06 8.1.1 0.057 0.424 65.45 8.1.0 0.076 0.404 65.32 8.0.30 0.082 0.329 64.39 8.0.29 0.063 0.366 64.64 8.0.28 0.058 0.352 64.50 8.0.27 0.080 0.347 64.38 8.0.26 0.074 0.354 64.75 8.0.25 0.080 0.340 64.80 8.0.24 0.047 0.361 64.93 8.0.23 0.066 0.363 64.55 8.0.22 0.080 0.377 64.55 8.0.21 0.053 0.354 64.40 8.0.20 0.086 0.358 64.64 8.0.19 0.062 0.357 64.71 8.0.18 0.071 0.360 64.91 8.0.17 0.086 0.330 64.63 8.0.16 0.063 0.363 64.50 8.0.15 0.051 0.362 64.36 8.0.14 0.058 0.411 64.45 8.0.13 0.058 0.418 64.45 8.0.12 0.056 0.422 64.70 8.0.11 0.046 0.487 64.63 8.0.10 0.062 0.462 64.49 8.0.9 0.059 0.428 64.69 8.0.8 0.060 0.416 64.54 8.0.7 0.060 0.431 64.54 8.0.6 0.057 0.417 64.78 8.0.5 0.054 0.429 64.70 8.0.3 0.052 0.427 64.47 8.0.2 0.074 0.507 64.74 8.0.1 0.078 0.538 64.72 8.0.0 0.063 0.561 64.77 7.4.33 0.054 0.405 64.27 7.4.32 0.070 0.369 64.54 7.4.30 0.066 0.399 64.11 7.4.29 0.072 0.365 64.48 7.4.28 0.077 0.390 64.15 7.4.27 0.054 0.453 64.35 7.4.26 0.052 0.467 64.36 7.4.25 0.059 0.454 64.22 7.4.24 0.058 0.480 64.03 7.4.23 0.060 0.453 64.25 7.4.22 0.056 0.464 64.34 7.4.21 0.068 0.445 64.27 7.4.20 0.053 0.479 64.35 7.4.19 0.061 0.450 64.41 7.4.18 0.063 0.449 64.33 7.4.16 0.056 0.466 64.44 7.4.15 0.065 0.554 64.19 7.4.14 0.080 0.561 63.96 7.4.13 0.060 0.573 64.24 7.4.12 0.080 0.557 64.31 7.4.11 0.072 0.572 64.09 7.4.10 0.064 0.627 64.29 7.4.9 0.080 0.631 64.22 7.4.8 0.076 0.628 64.11 7.4.7 0.091 0.716 64.18 7.4.6 0.080 0.636 64.21 7.4.5 0.095 0.695 64.21 7.4.4 0.092 0.728 64.12 7.4.3 0.083 0.634 64.21 7.4.2 0.076 0.653 64.19 7.4.1 0.069 0.687 64.17 7.4.0 0.079 0.663 64.15 7.3.33 0.058 0.448 64.14 7.3.32 0.060 0.517 62.57 7.3.31 0.057 0.457 64.02 7.3.30 0.059 0.457 63.92 7.3.29 0.053 0.462 64.07 7.3.28 0.049 0.489 64.04 7.3.27 0.068 0.625 64.11 7.3.26 0.055 0.580 64.07 7.3.25 0.078 0.557 64.16 7.3.24 0.072 0.570 64.12 7.3.23 0.064 0.593 64.05 7.3.22 0.071 0.623 64.06 7.3.21 0.073 0.651 64.00 7.3.20 0.083 0.652 64.11 7.3.19 0.081 0.665 64.18 7.3.18 0.081 0.660 64.16 7.3.17 0.101 0.739 64.14 7.3.16 0.082 0.797 64.08 7.3.15 0.096 0.643 64.18 7.3.14 0.077 0.624 64.17 7.3.13 0.080 0.721 64.20 7.3.12 0.079 0.679 64.14 7.3.11 0.091 0.775 64.08 7.3.10 0.081 0.761 64.03 7.3.9 0.079 0.706 64.14 7.3.8 0.097 0.769 64.08 7.3.7 0.079 0.682 64.12 7.3.6 0.076 0.677 64.13 7.3.5 0.075 0.677 64.19 7.3.4 0.074 0.706 64.13 7.3.3 0.066 0.710 64.19 7.3.2 0.131 0.725 64.24 7.3.1 0.122 0.684 64.65 7.3.0 0.111 0.761 64.65 7.2.34 0.076 0.689 64.06 7.2.33 0.091 0.783 64.09 7.2.32 0.070 0.818 63.98 7.2.31 0.077 0.738 63.96 7.2.30 0.087 0.869 64.06 7.2.29 0.093 0.844 63.91 7.2.28 0.074 0.784 64.12 7.2.27 0.083 0.813 64.05 7.2.26 0.074 0.885 63.99 7.2.25 0.077 0.834 64.07 7.2.24 0.079 0.797 64.09 7.2.23 0.084 0.803 64.01 7.2.22 0.083 0.858 64.04 7.2.21 0.073 0.786 64.07 7.2.20 0.085 0.781 64.14 7.2.19 0.073 0.808 64.08 7.2.18 0.086 0.778 64.02 7.2.17 0.068 0.796 64.18 7.2.16 0.069 0.811 64.13 7.2.15 0.122 0.846 64.62 7.2.14 0.101 0.782 64.67 7.2.13 0.096 0.806 64.72 7.2.12 0.107 0.832 64.65 7.2.11 0.102 0.799 64.64 7.2.10 0.115 0.781 64.58 7.2.9 0.099 0.784 64.59 7.2.8 0.108 0.791 64.63 7.2.7 0.100 0.798 64.67 7.2.6 0.108 0.801 64.66 7.2.5 0.113 0.829 64.60 7.2.4 0.123 0.951 64.56 7.2.3 0.108 0.858 64.77 7.2.2 0.104 0.921 64.73 7.2.1 0.104 0.844 64.56 7.2.0 0.107 0.842 64.72
preferences:dark mode live preview ace vim emacs key bindings
28.4 ms | 403 KiB | 5 Q