Skip to content

Instantly share code, notes, and snippets.

@reneeb
Created October 25, 2013 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reneeb/7159134 to your computer and use it in GitHub Desktop.
Save reneeb/7159134 to your computer and use it in GitHub Desktop.
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