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}
In reply to Re: Speed comparison between printing to STDOUT and printing to a filehandle
by shmem
in thread Speed comparison between printing to STDOUT and printing to a filehandle
by sberthold
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |