kwaping has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Speed reading (files)
by BrowserUk (Patriarch) on Aug 04, 2005 at 16:06 UTC | |
|
Re: Speed reading (files)
by blazar (Canon) on Aug 04, 2005 at 15:38 UTC | |
by kwaping (Priest) on Aug 04, 2005 at 15:49 UTC | |
by xdg (Monsignor) on Aug 04, 2005 at 16:14 UTC | |
|
Re: Speed reading (files)
by izut (Chaplain) on Aug 04, 2005 at 16:25 UTC | |
|
Re: Speed reading (files)
by anonymized user 468275 (Curate) on Aug 04, 2005 at 15:52 UTC | |
|
Re: Speed reading (files)
by polettix (Vicar) on Aug 04, 2005 at 17:10 UTC | |
|
Re: Speed reading (files)
by ikegami (Patriarch) on Aug 04, 2005 at 17:39 UTC | |
by kwaping (Priest) on Aug 04, 2005 at 17:48 UTC | |
by ikegami (Patriarch) on Aug 04, 2005 at 19:58 UTC | |
by BrowserUk (Patriarch) on Aug 05, 2005 at 04:30 UTC | |
by ikegami (Patriarch) on Aug 05, 2005 at 05:12 UTC | |
|