Is there any difference in assigning variable to itself after some actions on it or to another variable?
| From: | Vyacheslav Kondratyuk | Date: | Wed, 02 Jan 2013 12:31:51 +0000 |
| Subject: | Is there any difference in assigning variable to itself after some actions on it or to another variable? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-319992@lists.php.net to get a copy of this 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.