perl - array of integers using way too much memory?

后端 未结 8 690
我寻月下人不归
我寻月下人不归 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:12

    Or, implicitly handling the pack for you, there is Tie::Array::PackedC:

    use Tie::Array::PackedC;
    # make @arr use $arr_storage for storing packed elements, by default using 'l!' pack format
    tie my @arr, 'Tie::Array::PackedC', my $arr_storage;
    

    vec may also be of interest.

提交回复
热议问题