sberthold has asked for the wisdom of the Perl Monks concerning the following question:

Which way is faster?
open (FILE, ">$dir\\$file"); print FILE "this is text\n"; close FILE; or open (FILE, ">$dir\\$file"); *STDOUT = *FILE; print "this is text\n"; close FILE;
when I close FILE, the next time I use print will it be displaies on the screen?

2006-10-10 Retitled by GrandFather, as per Monastery guidelines
Original title: 'STDOUT'

  • Comment on Speed comparison between printing to STDOUT and printing to a filehandle
  • Download Code

Replies are listed 'Best First'.
Re: Speed comparison between printing to STDOUT and printing to a filehandle
by shmem (Chancellor) on Oct 10, 2006 at 20:53 UTC
    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:
    • terminal speed
    • buffering

    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}
      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
Re: Speed comparison between printing to STDOUT and printing to a filehandle
by dtr (Scribe) on Oct 10, 2006 at 20:57 UTC

    I don't know which will be faster, I very strongly suspect that there will be no difference at all between them. As previously mentioned, though, the only way to tell for sure would be to use the Benchmark module.

    With your second example, though, once you close the FILE filehandle, you will also have a closed STDOUT - which means that you will no longer see anything printed to STDOUT.

    You can get around this with the "local" operator, though. Something like this:-

    print "Hello World!\n"; { open (FILE, ">bob.txt"); local *STDOUT = *FILE; print "this is text\n"; close FILE; print "You will never see this\n"; } print "You will see this though\n";
Re: Speed comparison between printing to STDOUT and printing to a filehandle
by GrandFather (Saint) on Oct 10, 2006 at 20:44 UTC

    Why don't you try it? For checking speed you could try Benchmark.


    DWIM is Perl's answer to Gödel
Re: Speed comparison between printing to STDOUT and printing to a filehandle
by TGI (Parson) on Oct 10, 2006 at 23:14 UTC

    Is there any reason why you can't just do a normal print to a filehandle?

    print FILE "this is text\n";

    And if you must redirect the output of an unqualified print statement, why not just use select?

    select(FILE); print "Printing to FILE\n"; select(STDOUT); print "Printing to STDOUT\n";

    Typeglobs are cool and all, but I try to avoid messing with the symbol table whenever possible.


    TGI says moo

Re: Speed comparison between printing to STDOUT and printing to a filehandle
by aquarium (Curate) on Oct 11, 2006 at 02:29 UTC
    unless you have a real performance issue at hand or you're constrained otherwise; it's usually better to print to STDOUT in file processing scripts. This allows you to easily re-direct output to alternate files and to chain commands together with pipes for further processing. It's also a good idea to print debug output to STDERR in such scripts, so you can still chain scripts together without sending debug junk from one script to another. You can re-direct STDERR on the command line when running the script to another file etc.
    the hardest line to type correctly is: stty erase ^H