in reply to Speed comparison between printing to STDOUT and printing to a filehandle

With that little amount of text, there's no (measurable) difference between printing to STDOUT and printing to a file. It makes a huge difference if you have many, many lines to print. The reasons are:

If you print to STDOUT, you get at maximum the character processing speed of the terminal. Printing to a file, there is no such constraint.

Also, printing to STDOUT is line buffered, i.e. the output buffer is flushed after each line, which makes extra overhead. Printing to a file is block buffered, i.e. the content is flushed to the file when a data block (size as per your OS, generally a multiple of 512 Bytes) is full. So, printing to a file is generally faster.

When you close FILE the text won't get to STDOUT, it can't be printed. If you are clueful enough to use warnings or use the -w switch invoking perl, you get the warning

print() on closed filehandle FILE at line ...

<update>
Oops, I misread the post... *blush* - you are redirecting STDOUT. Well, my post may be useful anyways. - the overhead of *STDOUT = *FILE is exactly the time needed for that assignment. Now, if you do this a 1_000_000 times...
Thanks GrandFather for the hint via /msg :)
</update>

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Replies are listed 'Best First'.
Re^2: Speed comparison between printing to STDOUT and printing to a filehandle
by tilly (Archbishop) on Oct 11, 2006 at 00:05 UTC
    Technical nit. Printing to STDOUT is not always line buffered. If it can, Perl will check whether STDOUT is an interactive terminal, and will make it line buffered if it is, and otherwise will not.

    You can verify this for yourself under Unix by comparing the output of the following two commands:

    perl -e 'print "hi\n" while sleep 1' perl -e 'print "hi\n" while sleep 1' | perl -pe 1