Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Optimizing Output

by Juerd (Abbot)
on Apr 15, 2002 at 07:56 UTC ( #159113=note: print w/ replies, xml ) Need Help??


in reply to Optimizing Output

To optimize output...

  • never believe people who say sys* functions are always faster because they are more direct. syswrite is not faster than print, because print benefits from Perl's internal optimization. (syswrite vs print: print wins (buffered: 580%, unbuffered: 250%))
  • do not turn off buffering (do not turn on autoflush). A lot of people seem to have a habit of writing $|++; in every single script. Most scripts do not need it. Use $| wisely. (print '' unbuffered vs buffered: buffered wins (90%))
  • write large chunks if you are on a slow medium. If, for some reason, you have to write to a file on an operating system that does no buffering, buffer yourself, and write large chunks. For normal scripts, this is not much of a problem and writing directly is probably more efficient than building a chunk.
  • do not try to interpolate function calls as described in How do I expand function calls in a string? (perlfaq4), but if you do, use ${\ ... } instead of @{[ ... ]} - if you need list context, join it yourself (and remember that a constant literal is faster than $").

- Yes, I reinvent wheels.
- Spam: Visit eurotraQ.


Comment on Re: Optimizing Output
Re: Re: Optimizing Output
by ariels (Curate) on Apr 15, 2002 at 12:54 UTC

    "If, for some reason, you have to write to a file on an operating system that does no buffering, buffer yourself, and write large chunks."

    This is odd. For this level of output, you're not looking so much at operating system buffering, but at the buffering your run-time environment provides. For Perl, this is given by the "normal" output functions (not sysread and syswrite). For C, this is e.g. the stdio.h functions. See K&R for details on how to implement putc and getc. It all happens in user space, not kernel space.

    The vast majority of benefit from buffering comes from this application library level. In fact, the cited benchmarks say just that: switching off Perl's buffering (with $|=0) turns off this buffering; it doesn't do any hacking on obscure OS parameters. And doing it clobbers performance.


    Juerd below is, of course, correct. You switch buffering off by making your output "piping hot": $|=1.

      switching off Perl's buffering (with $|=0)

      The other way around. $| controls autoflush, the opposite of buffering.

      $| = 1; # Autoflush on, buffering off. $| = 0; # Autoflush off, buffering on. $|++; # $| = 1 $|--; # $| = !$| (flip setting 0/1)

      - Yes, I reinvent wheels.
      - Spam: Visit eurotraQ.
      

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (9)
As of 2014-09-22 08:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (185 votes), past polls