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

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.

Replies are listed 'Best First'.
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?

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2020-09-20 03:32 GMT
Find Nodes?
    Voting Booth?
    If at first I donít succeed, I Ö

    Results (117 votes). Check out past polls.