Getters and setters calltime

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);

	}

}



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.

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.

I’ve tested using same version of Yii (yii-1.1.8.r3324)

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.

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.

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 :rolleyes:

maybe it is twice as fast, but if we talking about 0.001 … :rolleyes:

Surely its some kind of code “idealization”, unneeded most of the time :)

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