RE: Pointers For Newbies, Reminders For Oldies

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> -----Original Message-----
> From: Rene Veerman [mailto:rene7705@xxxxxxxxx]
> Sent: 27 January 2010 22:46
> 
> And if your script needs to pass large (> 5Mb) arrays around to
> functions, be sure to use passing-by-reference; failing to do so can
> double your memory requirements,
> possibly hitting the ini_set('memory_lmit', ??)

Have you benchmarked this? PHP's copy-on-change philosophy means there shouldn't be much difference in memory terms -- so unless you actually expect to change the array's contents, you should pass by value.

As proof, I constructed this little test:

	function test($arg, $base_mem)
	{
		echo "Additional inside func = ", memory_get_usage()-$base_mem, "<br />\n";
	}

	$mem_start = memory_get_usage();
	
	$array = array_fill(1,1000,"The quick brown fox jumps over the lazy dog");
	$mem_array = memory_get_usage();
	
	echo "After array creation = ", $mem_array-$mem_start, "<br />\n";
	
	test($array, $mem_array);
	
	echo "Final = ", memory_get_usage()-$mem_start, "<br />\n";

Which produced:

	After array creation = 546104
	Additional inside func = 208
	Final = 546312	
	
Changing the function definition to have &$arg instead of just $arg changed this to:

	After array creation = 545984
	Additional inside func = 208
	Final = 546192

In other words, there was no difference in the memory used to call a function with a by-reference argument vs a by-value argument, but the by-value version occupied an additional 120 bytes overall (which is hardly going to break the bank!). (By varying the numbers in the array_fill(), I found some minor variations, but nothing significant.)

Much more interesting is adding the following line to the beginning of the function:

	$x = count($arg);

In this case, the by-value figures were:

	After array creation = 546104
	Additional inside func = 280
	Final = 546384

... but by-reference gave:

	After array creation = 545984
	Additional inside func = 64600
	Final = 610456

In other words, just using count() on the array increased memory usage for the by-*reference* version by over 60 kbytes, whereas the by-value version only went up by 72 bytes.

Finally, I ran a quick benchmark of runtimes for a version of the function that just returns a single element of the array, which doesn't incur the ballooning memory demonstrated above. For 10,000 calls to the function (in a for loop, and averaged over several runs), the respective times were:

	By value: 	 5.54sec
	By reference:	 5.64sec

Or, the by-value version was very, very slightly quicker.

All in all, there is no evidence that passing arrays to functions by value is inherently more expensive than by reference, and in fact in some cases it can be significantly cheaper. Which in turn says that you should only pass arguments by reference when you actually expect to change them, which is after all what the by-reference operator is there for in the first place! QED.

Cheers!

Mike

 -- 
Mike Ford,
Electronic Information Developer, Libraries and Learning Innovation,  
Leeds Metropolitan University, C507, Civic Quarter Campus, 
Woodhouse Lane, LEEDS,  LS1 3HE,  United Kingdom 
Email: m.ford@xxxxxxxxxxxxxx 
Tel: +44 113 812 4730




To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux