Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: Re: Re: Slurp a file

by jeroenes (Priest)
on Jan 08, 2001 at 21:59 UTC ( #50535=note: print w/replies, xml ) Need Help??

in reply to Re: Re: Slurp a file
in thread Slurp a file

Tilly pointed me to the use of join in this case. Methinks it's pretty easy:
$content = join '', <SOMEHANDLE>;
Chipmunk, just 4u: One might see the actual response of tilly in this node <grin>.

I was dreaming of guitarnotes that would irritate an executive kind of guy (FZ)

Replies are listed 'Best First'.
Re (tilly) 4: Slurp a file
by tilly (Archbishop) on Jan 08, 2001 at 22:52 UTC
    That will be slower though.

    The reason I suggested it is in case someone wanted to use your SuperSplit with tied filehandles. I would not depend on a tied filehandle correctly paying attention to $/, so the join makes sense.

    But if you care about performance or have good reason to believe that people won't use tied filehandles, then avoid join.

      For the best of both worlds, use both:

      my $contents= do { local($/); join '', <INPUT> };
      should work quickly for file handles that pay attention to $/ and work about as quickly as possible for those that don't.

              - tye (but my friends call me "Tye")
        Modified davorg's code accordingly. Some improvement, clearly:
        Benchmark: timing 50000 iterations of hybrid, join, slash... hybrid: 4 secs ( 2.90 usr 0.38 sys = 3.28 cpu) join: 6 secs ( 5.65 usr 0.35 sys = 6.00 cpu) slash: 3 secs ( 1.95 usr 0.38 sys = 2.33 cpu)

        I was dreaming of guitarnotes that would irritate an executive kind of guy (FZ)

Re: Re: Re: Re: Slurp a file
by merlyn (Sage) on Jan 08, 2001 at 22:03 UTC

      Yes they were :) But I'm not sure I like the results. Is there a problem with this benchmarking code?

      #!/usr/bin/perl -w use strict; use Benchmark; my $file = $0; open IN, $file or die "$file: $!\n"; sub joinit { my $content = join '', <IN>; } sub dollarslash { my $content = do { local $/; <IN> } } timethese(100_000, {join => \&joinit, slash => \&dollarslash} );

      because it implies that the join is faster...

      Benchmark: timing 100000 iterations of join, slash... join: 2 wallclock secs ( 1.52 usr + 0.29 sys = 1.81 CPU) slash: 4 wallclock secs ( 2.99 usr + 0.35 sys = 3.34 CPU)

      "Perl makes the fun jobs fun
      and the boring jobs bearable" - me

        Yes, you forgot to seek so most of the iterations don't read anything so you are only measuring overhead. (:

                - tye (but my friends call me "Tye")

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://50535]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2023-02-06 12:20 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (34 votes). Check out past polls.