RE: Pointers For Newbies, Reminders For Oldies

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

 



> -----Original Message-----
> From: Nathan Rixham [mailto:nrixham@xxxxxxxxx]
> Sent: 28 January 2010 13:43
> 
> Ford, Mike wrote:
> >> -----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";
> > 	}
> >
> 
> try changing this to access the array in some way such as:
> 
> function test($arg, $base_mem)
> {
>  foreach( $arg as $index => $value ) {
> 
>  }
>  echo "Additional= ", memory_get_usage()-$base_mem, "\n";
> }
> 
> After array creation = 52696
> Additional = 101152
> Final = 117200
> 
> vs: function test(&$arg, $base_mem)
> 
> After array creation = 52696
> Additional = 53104
> Final = 101696
> 
> there's the double memory usage

H'mm, that's interesting! I'm not surprised about foreach causing greater memory usage, as it's defined to operate on its own copy of the array. However, when I run the same test, I get:

	After array creation = 546104
	Additional inside func = 64504
	Final = 610336

Vs

	After array creation = 545984
	Additional inside func = 376
	Final = 546360

So here I agree that the reference version is less memory intensive, but I don't see anything like a doubling of memory even for the non-reference version. I guess it depends exactly what you do inside the function/loop. I also wonder if it's different for PHP versions -- I'm on 5.2.5.
 
I did originally do a test of straight access, such as $x = $arg[$i], with no significant effect on my results, but I didn't think about the foreach wrinkle. Out of interest, I've just run a test using a plain for loop (with a hardcoded limit, to avoid distortion caused by count()!!), which yields:

	Additional inside func = 328
and
	Additional inside func = 328

So it definitely looks like the foreach that's causing the memory bloat!

This all just goes to show that you can't always second-guess the best strategy, and, unless you're really tight for some resource, you probably might just as well program in the way that feels most natural to you!

Happy programming, one and all!

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