I have the 2 following codes.
1:
$i = 0;
while(1)
{
$i++;
echo "big text for memory usage ";
if ( $i == 50000 )
break;
}
echo "<br />" . memory_get_usage();
It echoes every time : 1626464
2:
$i = 0;
for(;;)
{
$i++;
echo "big text for memory usage ";
if ( $i == 50000 )
break;
}
echo "<br />" . memory_get_usage();
It echoes every time : 1626656
Can anybody exaplain this difference between the 2 different memory usages? Even if they are so small...
It's an implementation detail. With the for loop, PHP probably uses some space to store three pointers, one for the for intialization, one for the incrementation, and one for the stop condition. If you're on a 64-bit system, then this accounts for the 64 * 3 = 192 extra bits you're seeing. Of course, it's hard to tell if I'm right without looking at the actual code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With