Re: Pointers For Newbies, Reminders For Oldies

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

 



On Thu, 28 Jan 2010 10:02:56 -0500, robert@xxxxxxxxxxxxx (Robert Cummings) wrote:


>I don't know what you guys are doing wrong but the following should be 
>the correct behaviour:
>
><?php
>
>function get_memory( $init=false )
>{
>     static $base = null;
>     if( $base === null || $init )
>     {
>         $base = memory_get_usage();
>     }
>
>     return memory_get_usage() - $base;
>}
>
>function simple_access( $data )
>{
>     $foo = $data[100];
>     echo 'Memory: '.get_memory().' (simple access)'."\n";
>}
>
>function foreach_value_access( $data )
>{
>     foreach( $data as $value )
>     {
>         $foo = $value;
>         break;
>     }
>     echo 'Memory: '.get_memory().' (foreach value access)'."\n";
>}
>
>function foreach_key_access( $data )
>{
>     foreach( $data as $key => $value )
>     {
>         $foo = $key;
>         $foo = $value;
>         break;
>     }
>     echo 'Memory: '.get_memory().' (foreach key/value access)'."\n";
>}
>
>function modify_single_access( $data )
>{
>     $data[100] = str_repeat( '@', 10000 );
>     echo 'Memory: '.get_memory().' (modify single access)'."\n";
>}
>
>function modify_all_access( $data )
>{
>     for( $i = 0; $i < 1000; $i++ )
>     {
>         $data[$i] = str_repeat( '@', 10000 );
>     }
>
>     echo 'Memory: '.get_memory().' (modify all access)'."\n";
>}
>
>
>get_memory( true );
>
>$data = array();
>for( $i = 0; $i < 1000; $i++ )
>{
>     $data[$i] = str_repeat( '#', 10000 );
>}
>
>echo 'Memory: '.get_memory().' (data initialized)'."\n";
>
>simple_access( $data );
>foreach_value_access( $data );
>foreach_key_access( $data );
>modify_single_access( $data );
>modify_all_access( $data );
>
>?>
>
>I get the following output (PHP 5.2.11 from command-line):
>
>     Memory: 10160768 (data initialized)
>     Memory: 10161008 (simple access)
>     Memory: 10161104 (foreach value access)
>     Memory: 10161240 (foreach key/value access)
>     Memory: 10267312 (modify single access)
>     Memory: 20321576 (modify all access)
>
>I don't double up on memory consumption until I force the write onto 
>every element... this is expected because internally every array element 
>is individually subject to the Copy-On-Write (COW) principle since each 
>element is internally stored as a zval just like the array itself.

Thanks for this.  I was just revising a bit of code to insert some extra entries into an
array. Previously I had opened a new array, copied the data up to the insertion point into
it, put in the new entry, then copied the tail, then renamed the new array. After reading
this, I realise the correct procedure was to copy the existing data into a temporary
array, insert the new entry in the existing array, and then copy the tail from the copy
back into the working data.

This way only the data in the tail actually has to be copied, rather than the whole array.

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