Skip to content

Instantly share code, notes, and snippets.

@lyrixx
Created June 3, 2011 13:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lyrixx/1006311 to your computer and use it in GitHub Desktop.
Save lyrixx/1006311 to your computer and use it in GitHub Desktop.
PHP Bench
<?php
class baseDog
{
public $name;
}
class dog
{
protected $name;
public function getName()
{
return $this->name;
}
public function __get($key)
{
return $this->$key;
}
public function setName($name)
{
return $this->name = $name;
}
public function __set($key, $value)
{
return $this->$key = $value;
}
}
define('LOOP_MAX', 1000000);
// get via property
$dog = new baseDog();
$tGetPropertyStart = microtime(true);
for($i = 0; $i <= LOOP_MAX; $i++)
{
$dog->name;
}
$tGetProperty = microtime(true) - $tGetPropertyStart;
// get via get
$dog = new dog();
$tGetGetStart = microtime(true);
for($i = 0; $i <= LOOP_MAX; $i++)
{
$dog->getName();
}
$tGetGet = microtime(true) - $tGetGetStart;
// get via __get
$dog = new dog();
$tGet__GetStart = microtime(true);
for($i = 0; $i <= LOOP_MAX; $i++)
{
$dog->name;
}
$tGet__Get = microtime(true) - $tGet__GetStart;
// set via property
$dog = new baseDog();
$tSetPropertyStart = microtime(true);
for($i = 0; $i <= LOOP_MAX; $i++)
{
$dog->name = "dog";
}
$tSetProperty = microtime(true) - $tSetPropertyStart;
// set via set
$dog = new dog();
$tSetSetStart = microtime(true);
for($i = 0; $i <= LOOP_MAX; $i++)
{
$dog->setName("dog");
}
$tSetSet = microtime(true) - $tSetSetStart;
// set via __set
$dog = new dog();
$tSet__SetStart = microtime(true);
for($i = 0; $i <= LOOP_MAX; $i++)
{
$dog->name = "dog";
}
$tSet__Set = microtime(true) - $tSet__SetStart;
echo phpversion()."\n";
echo 'GET'."\n";
foreach (array('property' => $tGetProperty, 'get' => $tGetGet, '__get' => $tGet__Get) as $key => $value)
{
$ratio = $value / $tGetProperty;
echo sprintf('[%-8s] : %f', $key, $ratio)."\n";
}
echo 'SET'."\n";
foreach (array('property' => $tSetProperty, 'set' => $tSetSet, '__set' => $tSet__Set) as $key => $value)
{
$ratio = $value / $tSetProperty;
echo sprintf('[%-8s] : %f', $key, $ratio)."\n";
}
echo "\n\n";
<?php
$array = array(
0 => 'test',
1 => 'test',
2 => 'test',
5 => 'test',
);
define('LOOP_MAX', 1000000);
// Isset return false
$tIssetFalseStart = microtime(true);
for($i = 0; $i <= LOOP_MAX; $i++)
{
if (isset($array[4]))
{
$a = $array[4];
}
$a = null;
}
$tIssetFalse = microtime(true) - $tIssetFalseStart;
// Isset return true
$tIssetTrueStart = microtime(true);
for($i = 0; $i <= LOOP_MAX; $i++)
{
if (isset($array[0]))
{
$a = $array[0];
}
$a = null;
}
$tIssetTrue = microtime(true) - $tIssetTrueStart;
// @ return false
$tAtFalseStart = microtime(true);
for($i = 0; $i <= LOOP_MAX; $i++)
{
$a = @$array[4];
}
$tAtFalse = microtime(true) - $tAtFalseStart;
// @ return true
$tAtTrueStart = microtime(true);
for($i = 0; $i <= LOOP_MAX; $i++)
{
$a = @$array[0];
}
$tAtTrue = microtime(true) - $tAtTrueStart;
echo phpversion()."\n";
echo 'We test something not set'."\n";
foreach (array('isset' => $tIssetFalse, '@' => $tAtFalse) as $key => $value)
{
$ratio = $value / $tIssetFalse;
echo sprintf('[%-8s] : %f', $key, $ratio)."\n";
}
echo 'We test something set'."\n";
foreach (array('isset' => $tIssetTrue, '@' => $tAtTrue) as $key => $value)
{
$ratio = $value / $tIssetTrue;
echo sprintf('[%-8s] : %f', $key, $ratio)."\n";
}
echo "\n\n";
<?php
class A
{
private $a1 = array();
private $a2 = array();
}
class B
{
private $b1 = array();
private $b2;
public function __construct()
{
$this->b2 = array();
}
}
class C
{
private $c1;
private $c2;
public function __construct()
{
$this->c1 = array();
$this->c2 = array();
}
}
define('LOOP_MAX', 1000000);
// all as attribute
$tAStart = microtime(true);
for($i = 0; $i <= LOOP_MAX; $i++)
{
new A();
}
$tA = microtime(true) - $tAStart;
// one as attribute, one in construct
$tBStart = microtime(true);
for($i = 0; $i <= LOOP_MAX; $i++)
{
new B();
}
$tB = microtime(true) - $tBStart;
// all in construct
$tCStart = microtime(true);
for($i = 0; $i <= LOOP_MAX; $i++)
{
new C();
}
$tC = microtime(true) - $tCStart;
echo phpversion()."\n";
foreach (array('attribute' => $tA, 'both' => $tB, 'construct' => $tC) as $key => $value)
{
$ratio = $value / $tA;
echo sprintf('[%-10s] : %f', $key, $ratio)."\n";
}
echo "\n\n";
<?php
$arrayMax = 20000;
$array = array_combine(range(1, $arrayMax), range(1, $arrayMax));
$key = 11111;
define('LOOP_MAX', 10000);
// in_array
$tAStart = microtime(true);
for($i = 0; $i <= LOOP_MAX; $i++)
{
in_array($key, $array);
}
$tA = microtime(true) - $tAStart;
// isset
$tBStart = microtime(true);
for($i = 0; $i <= LOOP_MAX; $i++)
{
isset($array[$key]);
}
$tB = microtime(true) - $tBStart;
// array_key_exists
$tCStart = microtime(true);
for($i = 0; $i <= LOOP_MAX; $i++)
{
array_key_exists($key, $array);
}
$tC = microtime(true) - $tCStart;
echo phpversion()."\n";
foreach (array('in_array' => $tA, 'isset' => $tB, 'array_key_exists' => $tC) as $key => $value)
{
$ratio = $value / $tB;
echo sprintf('[%-16s] : %f', $key, $ratio)."\n";
}
echo "\n\n";
php 5.2.7 :
GET
[property] : 1.000000
[get ] : 3.696804
[__get ] : 5.063196
SET
[property] : 1.000000
[set ] : 3.940685
[__set ] : 4.804387
php 5.3.6
GET
[property] : 1.000000
[get ] : 1.585423
[__get ] : 2.785906
SET
[property] : 1.000000
[set ] : 1.941568
[__set ] : 2.744577
5.4.7-1~dotdeb.0
GET
[property] : 1.000000
[get ] : 3.943535
[__get ] : 5.130856
SET
[property] : 1.000000
[set ] : 3.944191
[__set ] : 4.770218
5.2.17-0.dotdeb.0
We test something not set
[isset ] : 1.000000
[@ ] : 3.427503
We test something set
[isset ] : 1.000000
[@ ] : 1.529751
5.3.6
We test something not set
[isset ] : 1.000000
[@ ] : 6.993796
We test something set
[isset ] : 1.000000
[@ ] : 3.275385
5.4.7-1~dotdeb.0
We test something not set
[isset ] : 1.000000
[@ ] : 2.819538
We test something set
[isset ] : 1.000000
[@ ] : 1.069074
5.2.17-0.dotdeb.0
[attribute ] : 1.000000
[both ] : 2.716186
[construct ] : 3.083696
5.3.6
[attribute ] : 1.000000
[both ] : 2.975947
[construct ] : 3.356322
5.4.7-1~dotdeb.0
[attribute ] : 1.000000
[both ] : 3.110702
[construct ] : 3.571433
5.3.17-1~dotdeb.0
[in_array ] : 635.614110
[isset ] : 1.000000
[array_key_exists] : 3.275343
5.2.17-0.dotdeb.0
[in_array ] : 1029.341540
[isset ] : 1.000000
[array_key_exists] : 3.287562
5.4.7-1~dotdeb.0
[in_array ] : 747.249286
[isset ] : 1.000000
[array_key_exists] : 3.251335
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment