Created
October 25, 2013 18:04
-
-
Save reneeb/7159134 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open my $fh, '<', $big_file or die; | |
for my $line ( <$fh> ) { # this reads the whole file into memory | |
# ... | |
} | |
vs. | |
open my $fh, '<', $big_file or die; | |
while ( my $line = <$fh> ) { # reads file line by line, uses less memory | |
# ... | |
} | |
my @big_array = ('all','data','in','one','big','array'); | |
my @filtered = grep{ $_ =~ m/a/ }@big_array; | |
# now two big arrays in memory | |
my @second_level = map{ $_ . " " . $_ }@filtered; | |
# now three big arrays in memory | |
vs. | |
my @filter; | |
{ | |
my @big_array = ('all','data','in','one','big','array'); | |
@filtered = grep{ $_ =~ m/a/ }@big_array; | |
} | |
my @second_level = map{ $_ . " " . $_ }@filtered; | |
# Perl now reuses memory as @big_array is out of scope in memory "freed" internally |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment