perl - 使用太多内存的整数数组? [英] perl - array of integers using way too much memory?

查看:21
本文介绍了perl - 使用太多内存的整数数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行以下脚本时:

my @arr = [1..5000000];

for($i=0; $i<5000000; $i++) {
        $arr[$i] = $i;
        if($i % 1000000 == 0) {
                print "$i\n";
        }
}

它消耗大约 500 MB 内存.习惯于更高级别的编译语言,我预计它大约为 5M * 4B = 20MB(每个数字 4 个字节).

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).

我猜这是因为每个值都是一个标量,而不是一个简单的二进制数.是否可以通过将这些值视为数字来减少内存占用,或者是否只有 500 MB 用于此任务?

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?

推荐答案

如果您正在处理如此大的数组,您可能需要使用像 PDL.

If you are dealing with such large arrays, you might want to use a toolkit like the PDL.

(哦,是的,你是对的:它需要太多内存,因为它是一个 Perl 标量数组.)

(Oh, and yes, you are correct: It takes so much memory because it is an array of Perl scalars.)

这篇关于perl - 使用太多内存的整数数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆