Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Fastest way to get input from a filehandle

by jesuashok (Curate)
on Oct 29, 2005 at 13:24 UTC ( #503860=perlquestion: print w/replies, xml ) Need Help??
jesuashok has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
(i) $line = <STDIN> (ii) $line = readline(STDIN) (iii) $line = readline(*STDIN) (iv) open my $fh, "<&=STDIN" or die; $line = readline($fh);

monks I need your valuable suggestions on the performance above statements. functionality wise all are same. But I need to know which one is very efficient. I am expecting your valuable inputs.

"Keep pouring your ideas"

2005-10-29 Retitled by davido, as per Monastery guidelines
Original title: 'which one is Faster'

Replies are listed 'Best First'.
Re: Fastest way to get input from a filehandle
by sauoq (Abbot) on Oct 29, 2005 at 13:51 UTC

    They should all be similar enough that it shouldn't matter. Note in particular:

    $ perl -MO=Deparse -e '$line = readline(*STDIN)' $line = <STDIN>; -e syntax OK
    If you are really concerned about it, you should be using read() or sysread() instead.

    "My two cents aren't worth a dime.";
Re: Fastest way to get input from a filehandle
by japhy (Canon) on Oct 29, 2005 at 15:15 UTC
    I would expect the fourth one is the slowest, since you're going out of your way to get a line. The first three are, I believe, identical.

    But this is not where you should be looking for speed increases in your code. This is such a minor issue. You're over-optimizing.

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

      I would think the 2nd would be slowest since there is an extra step of getting the right filehandle from the string...

      $ perl -MO=Deparse -e 'readline(STDIN)' readline 'STDIN'; -e syntax OK
      Not sure when that happens though. (One time penalty?)

      I would expect the fourth to be about as quick as the others (after the open) as it's just dup'ing STDIN. Is it slower to store a filehandle in a lexical? (I wouldn't think so but have no real clue.)


      Not that any of this changes the fact that it shouldn't matter to the OP. :-)

      "My two cents aren't worth a dime.";
Re: Fastest way to get input from a filehandle
by bageler (Hermit) on Oct 29, 2005 at 15:59 UTC
    find out for yourself, using a static file, the Benchmark module and the exported timethese() subroutine. There are many examples of its usage on this site.
Re: Fastest way to get input from a filehandle
by pg (Canon) on Oct 29, 2005 at 16:28 UTC

    To be frank, the way you read does not make any difference in this context. In this case, the time taking a human being to response and type is much longer than the read takes.

      Of course, if the question is being asked in the context of reading STDIN from a pipe (rather than from a keyboard), then there could be a good reason to figure out whether one method of reading is faster or slower than another, and a benchmark test would be worthwhile -- even if all it does is prove that there's hardly any difference.

      And in that regard, the Benchmark module probably isn't necessary or even appropriate; the unix "time" command would probably do. Just put together a suitable test script that reads and processes data from STDIN, but accepts a command-line option to determine what sort of syntax to use for reading, then run a series of commands like:

      feeder_process | time test-perl-reader diamond > /dev/null # (repeat several times, average the results) feeder_process | time test-perl-reader readline1 > /dev/null # (repeat, average the results) feeder_process | time test-perl-reader readline2 > /dev/null # (you know the drill...)
      If the difference among the various averages is greater than the variance among test runs for any single method, then maybe there's a real difference in the efficiency of the different input methods.

      But I would expect any differences to be a very small fraction of the overall pipeline time.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://503860]
Approved by ww
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2017-10-22 10:25 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (272 votes). Check out past polls.