open(FILE,"<$file") || die $!; read FILE, my $data, -s $file; close(FILE);
I expected the second method (a traditional slurp) to be faster. However, I was surprised to find that the read function was almost twice as fast!open(FILE,"<$file") || die $!; my $data = do { local $/; <FILE> }; close(FILE);
#!/usr/bin/perl use strict; use warnings; use Time::HiRes qw(time); $| = 1; my $file = '/path/to/file.pdf'; my $numtests = 1000; print "using read function\n"; for (my $x = 0; $x < 6; $x++) { my $start = time(); for (my $i = 0; $i <= $numtests; $i++) { open(FILE,"<$file") || die $!; read FILE, my $data, -s $file; close(FILE); } my $end = time(); print $end - $start,"\n"; } print "\ntradtional slurp\n"; for (my $x = 0; $x < 6; $x++) { my $start = time(); for (my $i = 0; $i <= $numtests; $i++) { open(FILE,"<$file") || die $!; my $data = do { local $/; <FILE> }; close(FILE); } my $end = time(); print $end - $start,"\n"; } # exit; #<- removed for blazar ;)
using read function 0.612063884735107 0.62070107460022 0.599463939666748 0.60235595703125 0.610018014907837 0.603386878967285 tradtional slurp 1.03461217880249 1.0298318862915 1.05549097061157 1.08192896842957 1.02464509010315 1.0180230140686
In reply to Speed reading (files) by kwaping
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |