Peter Lind wrote:
This is one example where references actually decrease memory usage.
The main reason is the recursive nature of the function. Try

<?php

echo memory_get_usage() . PHP_EOL;
$array = range(0,1000000);
$array[10] = range(0,10);
$array[20] = range(0,10);
$array[30] = range(0,10);
$array[40] = range(0,10);
$array[50] = range(0,10);
$array[60] = range(0,10);
$array[70] = range(0,10);
$array[80] = range(0,10);
$array[90] = range(0,10);
$array[100] = range(0,10);
echo memory_get_usage() . PHP_EOL;
carray($array);
function carray ($array)
{
    foreach ($array as $value)
    {
        if (is_array($value)) carray($value);
    }
    echo memory_get_usage() . PHP_EOL;
    echo count($array) . PHP_EOL;
}
echo memory_get_usage() . PHP_EOL;

And then compare with:

<?php

echo memory_get_usage() . PHP_EOL;
$array = range(0,1000000);
$array[10] = range(0,10);
$array[20] = range(0,10);
$array[30] = range(0,10);
$array[40] = range(0,10);
$array[50] = range(0,10);
$array[60] = range(0,10);
$array[70] = range(0,10);
$array[80] = range(0,10);
$array[90] = range(0,10);
$array[100] = range(0,10);
echo memory_get_usage() . PHP_EOL;
carray($array);
function carray (&$array)
{
    $i = 0;
    foreach ($array as $value)
    {
        if (is_array($value)) carray($value);
    }
    echo memory_get_usage() . PHP_EOL;
    echo count($array) . PHP_EOL;
}
echo memory_get_usage() . PHP_EOL;

The memory usage spikes in the first example when you hit the second
array level - you don't see the same spike in the second example.

Regards
Peter

Doh, forgot about that :)

Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP

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

Reply via email to