evgeny.l
(Lexunin)
September 30, 2011, 3:54am
1
Some raw numbers (php v5.3.2, yii-1.1.8.r3324):
Comparing getters to normal call $this->param
getterByFuncCall | getterCall
161.0600% | 503.0311%
--------------------------------------------------
Comparing setters to normal call $this->param=$value
setterByFuncCall | setterCall
176.4807% | 540.9720%
Make your own conclusions.
Class i was using:
<?php
class Test extends CComponent
{
public $test=10;
public function getTestGetter()
{
return $this->test;
}
public function setTestSetter($value)
{
$this->test=$value;
}
public function runTest()
{
echo '<pre>';
$this->testGetters();
echo "\n".str_repeat('-', 50)."\n";
$this->testSetters();
echo '</pre>';
}
public function testSetters()
{
$count=1000;
$diff=0;
$diff2=0;
for ($i=0;$i<$count;$i++)
{
$start_time=microtime();
$this->test=10;
$call_time1=microtime()-$start_time;
$start_time=microtime();
$this->setTestSetter(10);
$call_time2=microtime()-$start_time;
$start_time=microtime();
$this->testSetter=10;
$call_time3=microtime()-$start_time;
$diff+=($call_time2/$call_time1)*100;
$diff2+=($call_time3/$call_time1)*100;
// echo sprintf('%.7f | %.7f | %.7f',$call_time1,$call_time2,$call_time3)."\n";
}
$diff=$diff/$count;
$diff2=$diff2/$count;
echo "Comparing setters to normal call \$this->param=\$value\n";
echo "setterByFuncCall | setterCall \n";
echo sprintf('% 15.4f%% | %.4f%%',$diff,$diff2);
}
public function testGetters()
{
$count=1000;
$diff=0;
$diff2=0;
for ($i=0;$i<$count;$i++)
{
$start_time=microtime();
$this->test;
$call_time1=microtime()-$start_time;
$start_time=microtime();
$this->getTestGetter();
$call_time2=microtime()-$start_time;
$start_time=microtime();
$this->testGetter;
$call_time3=microtime()-$start_time;
$diff+=($call_time2/$call_time1)*100;
$diff2+=($call_time3/$call_time1)*100;
// echo sprintf('%.7f | %.7f | %.7f',$call_time1,$call_time2,$call_time3)."\n";
}
$diff=$diff/$count;
$diff2=$diff2/$count;
echo "Comparing getters to normal call \$this->param\n";
echo "getterByFuncCall | getterCall \n";
echo sprintf('% 15.4f%% | %.4f%%',$diff,$diff2);
}
}
Van_Damm
(Van Damm)
September 30, 2011, 8:03am
2
Different results on PHP 5.2.17:
Comparing getters to normal call $this->param
getterByFuncCall | getterCall
170.7030% | 312.2490%
--------------------------------------------------
Comparing setters to normal call $this->param=$value
setterByFuncCall | setterCall
180.1255% | 324.8575%
I wonder why 5.3 performs worse on magic methods…
UPD: The test was performed on my local computer.
On my server, however, PHP 5.3.5 produces following results: (UPD: with eAccelerator enabled)
Comparing getters to normal call $this->param
getterByFuncCall | getterCall
87.2696% | 177.2294%
--------------------------------------------------
Comparing setters to normal call $this->param=$value
setterByFuncCall | setterCall
81.4767% | 147.5533%
PHP 5.3.8 on my local computer provides these results:
Comparing getters to normal call $this->param
getterByFuncCall | getterCall
119.1570% | 174.0040%
--------------------------------------------------
Comparing setters to normal call $this->param=$value
setterByFuncCall | setterCall
121.6583% | 176.9236%
So I think something is wrong with your results.
evgeny.l
(Lexunin)
September 30, 2011, 8:12am
3
Very interesting. Added to post version of yii, just in case.
Do you have any accelerators on server?
Cause if not, then:
getterByFuncCall 87.2696%
setterByFuncCall 81.4767%
means that calling function and assignment is faster then just only assignment.
Or we doing something wrong.
Van_Damm
(Van Damm)
September 30, 2011, 8:14am
4
I’ve tested using same version of Yii (yii-1.1.8.r3324)
evgeny.l
(Lexunin)
September 30, 2011, 8:48am
5
Pretty fast server (php v5.2.17 with eAccelerator v0.9.6.1):
Comparing getters to normall call $this->param
getterByFuncCall | getterCall
107.6083% | 186.9583%
--------------------------------------------------
Comparing setters to normall call $this->param=$value
setterByFuncCall | setterCall
126.3643% | 217.0667%
Looks like despite the fact that results a relative,
it significantly depends on computers hardware.
Van_Damm
(Van Damm)
September 30, 2011, 8:50am
6
Do you have any accelerators on server?
Cause if not, then:
getterByFuncCall 87.2696%
setterByFuncCall 81.4767%
means that calling function and assignment is faster then just only assignment.
Or we doing something wrong.
Yeah, you’re right. I’ve forgotten that I have eAccelerator installed on this server.
However, no opcode cacher is set up on my local computer and still results are different.
mikl
(Mike)
September 30, 2011, 1:20pm
7
The big question to me is rather, how much impact this really has, compared to the rest of application code. I would expect that in standard situations these differences are neglectible compared to e.g. DB access times. So, you should try to set up your benchmark in a way that it compares the overall impact in a standard scenario. I doubt, you’ll notice more than 0.0x % difference
evgeny.l
(Lexunin)
October 1, 2011, 3:42pm
8
The big question to me is rather, how much impact this really has, compared to the rest of application code. I would expect that in standard situations these differences are neglectible compared to e.g. DB access times. So, you should try to set up your benchmark in a way that it compares the overall impact in a standard scenario. I doubt, you’ll notice more than 0.0x % difference
You are right. Its just a small test for speed comparing.
But it’s obviously, that in large cycles better not to use them,
or use callByFunc instead of clean call.
Would be better if you show some time
maybe it is twice as fast, but if we talking about 0.001 …
evgeny.l
(Lexunin)
October 2, 2011, 7:30am
10
Surely its some kind of code “idealization”, unneeded most of the time
dckurushin
(Diavolonok)
October 3, 2011, 9:04am
11
ideal for me is not only time issue, cause Yii is memory and speed eater compared to raw procedural coding
The simple blog exmaple consume 17mb of memory
and it is very slow compared to the same blog via procedural approach