php-general Digest 2 Jan 2013 12:31:56 -0000 Issue 8083
Topics (messages 319992 through 319992):
Is there any difference in assigning variable to itself after some actions on
it or to another variable?
319992 by: Vyacheslav Kondratyuk
Administrivia:
To subscribe to the digest, e-mail:
php-general-digest-subscr...@lists.php.net
To unsubscribe from the digest, e-mail:
php-general-digest-unsubscr...@lists.php.net
To post to the list, e-mail:
php-gene...@lists.php.net
----------------------------------------------------------------------
--- Begin Message ---
Hi,
Not long ago I had a debates about assignment variables to itself after
some action on them.
Assign variable to itself [AI]:
echo memory_get_usage() . "\n"; // 645680
$repeatedString = str_repeat('Hello,', 10000);
echo memory_get_usage() . "\n"; // 705944, AI_delta1 = 60264
$repeatedString = explode(',', $repeatedString);
echo memory_get_usage() . "\n"; // 3337888, AI_delta2 = 2631944
echo memory_get_peak_usage() . "\n"; // AI_peak = 3401024
Assign variable to another variable [AAV]:
echo memory_get_usage() . "\n"; // 645752
$repeatedString = str_repeat('Hello,', 10000);
echo memory_get_usage() . "\n"; // 706024, AAV_delta1 = 60272
$explodedString = explode(',', $repeatedString);
echo memory_get_usage() . "\n"; // 3398256, AAV_delta2 = 2692232
echo memory_get_peak_usage() . "\n"; // AAV_peak = 3400984
I created tests of memory usage in both cases and subtract values:
AAV_delta1 - AI_delta = 8
AAV_delta2 - AI_delta2 = 60288
AAV_peak - AI_peak = -40
According to this results it doesn't matter which approach to use, memory
usage - same. It's only a question should I use variable $repeatedString at
my code below or not.
Am I right at my conclusions or my tests are not correct?
Why it happens this way?
Also a question: AAV_delta1 - AI_delta = 8, I expect it should be equal 0.
Why it's equals 8?
Note: Memory usage can vary on your system.
PHP Version: 5.3.5-1ubuntu7.11.
--- End Message ---