perl - array of integers using way too much memory?

后端 未结 8 699
我寻月下人不归
我寻月下人不归 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条回答
  •  梦毁少年i
    2021-01-18 06:24

    You can always use C or C++ in Perl.This will probably give you a small footprint in some hard jobs. Just an idea using C!

    #!/usr/bin/perl
    use Inline C;
    use strict;
    
    for(my $i=0; $i<5000000; $i++) {
            set_array_index($i,$i);
            if($i % 1000000 == 0) {
                    #print "$i\n";
                    print get_array_index($i)."\n";
            }
    }
    
    __END__
    __C__
    
    int array[5000000];
    
    void set_array_index(int index,int value) {
        array[index]=value;
    }
    
    int get_array_index(int index) {
    
        if (array[index]==NULL)
            return 0;
    
        return array[index];
    }
    

提交回复
热议问题