perl - array of integers using way too much memory?

后端 未结 8 694
我寻月下人不归
我寻月下人不归 2021-01-18 06:05

When I run the following script:

my @arr = [1..5000000];

for($i=0; $i<5000000; $i++) {
        $arr[$i] = $i;
        if($i % 1000000 == 0) {
                    


        
8条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-18 06:27

    Maybe you could use an iterator instead of such a big list of integers.

    The iterator pays an overhead of a function call for each new value, but saves memory. Check MJD Higher Order Perl Chapter 4 ( 4.2.1 ).

    If I remember it right, range operator dont' build such a huge list in latest perls.

提交回复
热议问题