When I run the following script:
my @arr = [1..5000000];
for($i=0; $i<5000000; $i++) {
$arr[$i] = $i;
if($i % 1000000 == 0) {
print "$i\n";
}
}
It consumes about 500 MB memory. Used to higher-level compiled languages I would expect it to be roughly 5M * 4B = 20MB (4 bytes per number).
I guess that is because each value is a scalar, not a simple binary number. Is it possible to decrease memory footprint by treating those values as numbers, or is 500 MB for this task the only way?
my @arr = [1..5000000];is a mistake and doesn't do what you want it to. You'd probably see less usage if you wrotemy @arr = (1..5000000);. Or simplymy @arr;since you're not using any of the values you initialize with.