Skip to content

Instantly share code, notes, and snippets.

@markguinn
Last active December 28, 2015 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markguinn/7502041 to your computer and use it in GitHub Desktop.
Save markguinn/7502041 to your computer and use it in GitHub Desktop.
Very unscientific benchmark of PHP 5.4 traits against Silverstripe Extensions at mixed-in function calls. Results on my late 2011 MBP: Traits are between 10 and 40 times faster.
<?php
/**
Results on my Macbook:
Native function (no Object, no traits): 0.12234592
Native function (no Object, traits): 0.13018894
Native function (traits): 0.15913486
Native function (extension): 0.12871218
Mixin function (traits): 0.12146902
Mixin function (extension): 4.077214
Random functions (traits): 0.46887112
Random functions (extensions): 4.22944283
**/
class timer {
var $start;
var $pause_time;
/* start the timer */
function __construct($start = 0) {
if($start) { $this->start(); }
}
/* start the timer */
function start() {
$this->start = $this->get_time();
$this->pause_time = 0;
}
/* pause the timer */
function pause() {
$this->pause_time = $this->get_time();
}
/* unpause the timer */
function unpause() {
$this->start += ($this->get_time() - $this->pause_time);
$this->pause_time = 0;
}
/* get the current timer value */
function get($decimals = 8) {
return round(($this->get_time() - $this->start),$decimals);
}
/* format the time in seconds */
function get_time() {
list($usec,$sec) = explode(' ', microtime());
return ((float)$usec + (float)$sec);
}
}
class A extends ViewableData {
use tC, tD, tE, tF, tG, tH;
public $count = 0;
function f1() { return $this->count++; }
}
class AA {
use tC, tD, tE, tF, tG, tH;
public $count = 0;
function f1() { return $this->count++; }
}
class AAA {
public $count = 0;
function f1() { return $this->count++; }
}
class B extends ViewableData {
private static $extensions = array('C','D','E','F','G','H');
public $count = 0;
function f1() { return $this->count++; }
}
class C extends Extension {
public function f2() { return $this->owner->count++; }
}
class D extends Extension {
public function f3() { return $this->owner->count++; }
}
class E extends Extension {
public function f4() { return $this->owner->count++; }
}
class F extends Extension {
public function f5() { return $this->owner->count++; }
}
class G extends Extension {
public function f6() { return $this->owner->count++; }
}
class H extends Extension {
public function f7() { return $this->owner->count++; }
}
trait tC {
function f2() { return $this->count++; }
}
trait tD {
function f3() { return $this->count++; }
}
trait tE {
function f4() { return $this->count++; }
}
trait tF {
function f5() { return $this->count++; }
}
trait tG {
function f6() { return $this->count++; }
}
trait tH {
function f7() { return $this->count++; }
}
class MyBenchmarks extends Controller
{
private static $allowed_actions = ['index'];
function index() {
echo "Running some crazy benchmarks:<br>\n";
$t = new timer();
$numRuns = 1000000;
$t->start();
$obj = new AAA;
for ($i = 0; $i < $numRuns; $i++) $obj->f1();
echo "Native function (no Object, no traits): " . $t->get() . "<br>\n";
$t->start();
$obj = new AA;
for ($i = 0; $i < $numRuns; $i++) $obj->f1();
echo "Native function (no Object, traits): " . $t->get() . "<br>\n";
$t->start();
$obj = new A;
for ($i = 0; $i < $numRuns; $i++) $obj->f1();
echo "Native function (traits): " . $t->get() . "<br>\n";
$t->start();
$obj = new B;
for ($i = 0; $i < $numRuns; $i++) $obj->f1();
echo "Native function (extension): " . $t->get() . "<br>\n";
$t->start();
$obj = new A;
for ($i = 0; $i < $numRuns; $i++) $obj->f3();
echo "Mixin function (traits): " . $t->get() . "<br>\n";
$t->start();
$obj = new B;
for ($i = 0; $i < $numRuns; $i++) $obj->f3();
echo "Mixin function (extension): " . $t->get() . "<br>\n";
$t->start();
$obj = new A;
for ($i = 0; $i < $numRuns; $i++) {
$fn = 'f' . rand(1, 7);
$obj->$fn();
}
echo "Random functions (traits): " . $t->get() . "<br>\n";
$t->start();
$obj = new B;
for ($i = 0; $i < $numRuns; $i++) {
$fn = 'f' . rand(1, 7);
$obj->$fn();
}
echo "Random functions (extensions): " . $t->get() . "<br>\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment