Maugrim_The_Reaper wrote:has anyone using this actually measured the gain???
I've just done a few benchmarks and it's negligible at best. I wanted to measure the actual memory usage and time taken for just one call to the method but xdebug is whining about undefined functions on the server I have access to.
Speed comparisons calling the methods:
Code: Select all
<?php
class StaticGainsTest
{
private function getNull()
{
return null;
}
public function getLotsOfNull()
{
$ret = array();
for ($i = 0; $i < 100000; $i++)
{
$ret[] = $this->getNull();
}
return $ret;
}
}
$start = microtime(true);
$o = new StaticGainsTest();
$o->getLotsOfNull();
$end = microtime(true);
echo "Called non-static method 100000 times in ". ($end - $start) . " seconds\n";
?>
Code: Select all
Called non-static method 100000 times in 6.48325991631 seconds
Code: Select all
<?php
class StaticGainsTest
{
private static function getNull()
{
return null;
}
public function getLotsOfNull()
{
$ret = array();
for ($i = 0; $i < 100000; $i++)
{
$ret[] = self::getNull();
}
return $ret;
}
}
$start = microtime(true);
$o = new StaticGainsTest();
$o->getLotsOfNull();
$end = microtime(true);
echo "Called static method 100000 times in ". ($end - $start) . " seconds\n";
?>
Code: Select all
Called static method 100000 times in 6.25048303604 seconds
Speed comparisons creating the objects:
Code: Select all
<?php
class StaticGainsTest
{
private function getNull()
{
return null;
}
public function getLotsOfNull()
{
$ret = array();
for ($i = 0; $i < 100000; $i++)
{
$ret[] = $this->getNull();
}
return $ret;
}
}
$start = microtime(true);
$collection = array();
for ($i = 0; $i < 10000; $i++)
{
$collection[] = new StaticGainsTest();
}
$end = microtime(true);
echo "Created a collection of 10000 objects with a no static methods in " . ($end-$start) . " seconds\n";
?>
Code: Select all
Created a collection of 10000 objects with a no static methods in 0.0555810928345 seconds
Code: Select all
<?php
class StaticGainsTest
{
private static function getNull()
{
return null;
}
public function getLotsOfNull()
{
$ret = array();
for ($i = 0; $i < 100000; $i++)
{
$ret[] = self::getNull();
}
return $ret;
}
}
$start = microtime(true);
$collection = array();
for ($i = 0; $i < 10000; $i++)
{
$collection[] = new StaticGainsTest();
}
$end = microtime(true);
echo "Created a collection of 10000 objects with a static method in " . ($end-$start) . " seconds\n";
?>
Code: Select all
Created a collection of 10000 objects with a static method in 0.028272151947 seconds
Memory comparisons calling the methods:
Code: Select all
<?php
class StaticGainsTest
{
private function getNull()
{
return null;
}
public function getLotsOfNull()
{
$ret = array();
for ($i = 0; $i < 100000; $i++)
{
$ret[] = $this->getNull();
}
return $ret;
}
}
$o = new StaticGainsTest();
$o->getLotsOfNull();
echo "Ran non-static method 100000 times using " . (xdebug_peak_memory_usage()) . " bytes in memory\n";
?>
Code: Select all
Ran non-static method 100000 times using 6164328 bytes in memory
Code: Select all
<?php
class StaticGainsTest
{
private static function getNull()
{
return null;
}
public function getLotsOfNull()
{
$ret = array();
for ($i = 0; $i < 100000; $i++)
{
$ret[] = self::getNull();
}
return $ret;
}
}
$o = new StaticGainsTest();
$o->getLotsOfNull();
echo "Ran static method 100000 times using " . (xdebug_peak_memory_usage()) . " bytes in memory\n";
?>
Code: Select all
Ran static method 100000 times using 6164392 bytes in memory
Memory comparisons creating the objects:
Code: Select all
<?php
class StaticGainsTest
{
private function getNull()
{
return null;
}
public function getLotsOfNull()
{
$ret = array();
for ($i = 0; $i < 100000; $i++)
{
$ret[] = $this->getNull();
}
return $ret;
}
}
$collection = array();
for ($i = 0; $i < 10000; $i++)
{
$collection[] = new StaticGainsTest();
}
echo "Created a collection of 10000 objects with a no static methods using " . (xdebug_peak_memory_usage()) . " bytes in memory\n";
?>
Code: Select all
Created a collection of 10000 objects with a no static methods using 1994504 bytes in memory
Code: Select all
<?php
class StaticGainsTest
{
private static function getNull()
{
return null;
}
public function getLotsOfNull()
{
$ret = array();
for ($i = 0; $i < 100000; $i++)
{
$ret[] = self::getNull();
}
return $ret;
}
}
$collection = array();
for ($i = 0; $i < 10000; $i++)
{
$collection[] = new StaticGainsTest();
}
echo "Created a collection of 10000 objects with a static method using " . (xdebug_peak_memory_usage()) . " bytes in memory\n";
?>
Code: Select all
Created a collection of 10000 objects with a static method using 1994576 bytes in memory