*blushes*
The first and the second times are to be expected to be different. The third should be the same as the second. The reason for the difference is that on the first time all it has to do is allocate the space for the array and the strings. The second and third trys have to deallocate first and then suffer the same overhead.
However, I believe you have stumbled on a bug. When I change the context to scalar the times are predicatable. For instance your code on an 8.5MB file it takes 13 seconds for the first try and more minutes than I am willing to wait on the next two. When i replace it with a slurp and a plit it becomes 2 seconds and 5 seconds. When i replace it with a while (<$fh>) it goes to 4 and 10. Heres the code:
use strict;
use warnings;
$|++;
my @lines;
for my $try (1..3) {
print "Try $try\n";
my $start=time;
@lines=();
# 6 million ways to die, choose one:
# my_slurp(\@lines);
# my_while(\@lines);
# my_file(\@lines);
print "Took ".(time-$start)." seconds.\n";
}
sub my_file
{
my ($line) = @_;
open my $fh, "D:\\Perl\\DevLib\\Scrabble\\dict.txt";
@{$line}= <$fh>;
close($fh);
}
sub my_slurp {
my ($line) = @_;
local $/;
open my $fh, "D:\\Perl\\DevLib\\Scrabble\\dict.txt";
@{$line}=split /\n/,<$fh>;
close($fh);
}
sub my_while {
my ($line) = @_;
open my $fh, "D:\\Perl\\DevLib\\Scrabble\\dict.txt";
push @$line,$_ while <$fh>;
close($fh);
}
__END__
my_file:
Try 1
Took 13 seconds.
Try 2
^C (I killed it after 5 minutes.)
my_while:
Try 1
Took 4 seconds.
Try 2
Took 10 seconds.
Try 3
Took 9 seconds.
my_slurp:
Try 1
Took 2 seconds.
Try 2
Took 6 seconds.
Try 3
Took 5 seconds.
Hopefully someone with 5.8 or bleadperl can check if this is a resolved bug, if not it definately should be reported.
Sorry I didnt try your code properly on my first reply. :-)
Dont forget the resolution on the timings is at least +-1 second.
Also, have you considered that maybe there is a better strategy than reading the whole file in three times? Do you have to read it all in at once? Why cant you just copy the data once its read? This doesnt resolve the bug, but it sounds like design improvements are not out of order.
--- demerphq
my friends call me, usually because I'm late....
|