*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....


In reply to Re: Re: Re: Re: Re: opening a file in a subroutine by demerphq
in thread opening a file in a subroutine by abhishes

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.