Here is the benchmark that tests two things: buffered vs unbuffered code
and multi-statement print vs single statement print styles. Here are the
code and results: 

  use Symbol;
  my $fh = gensym;
  open $fh, ">/dev/null" or die;
  use Benchmark;

  sub multi_print{
    print $fh "<HTML>";
    print $fh "<HEAD>";
    print $fh "<TITLE>";
    print $fh "Test page";
    print $fh "<TITLE>";
    print $fh "</HEAD>";
    print $fh "<BODY BGCOLOR=\"black\" TEXT=\"white\">";
    print $fh "<H1>";
    print $fh "Hello";
    print $fh "</H1>";
    print $fh "<A HREF=\"foo.html\">";
    print $fh "foo";
    print $fh "</A>";
    print $fh "</BODY>";
    print $fh "</HTML>";
  }

  sub single_print{
    print $fh qq{<HTML>
      <HEAD>
        <TITLE>Test page<TITLE>
      </HEAD>
      <BODY BGCOLOR="black" TEXT="white">
        <H1>
          Hello
        </H1>
        <A HREF="foo.html">foo</A>
      </BODY>
    </HTML>
    };
  }

  timethese
    (500_000, {
           multi_print_unbuffered  => sub {local $| = 1; multi_print() ;},
           multi_print_buffered    => sub {              multi_print() ;},
           single_print_unbuffered => sub {local $| = 1; single_print();},
           single_print_buffered   => sub {              single_print();},
          });

Benchmark: timing 500000 iterations of multi_print_buffered,
multi_print_unbuffered, single_print_buffered, single_print_unbuffered...
multi_print_buffered: 13 wallclock secs (11.09 usr +  0.17 sys = 11.26
CPU)
multi_print_unbuffered: 18 wallclock secs (16.50 usr +  0.31 sys = 16.81
CPU)
single_print_buffered:  2 wallclock secs ( 2.83 usr +  0.06 sys =  2.89
CPU)
single_print_unbuffered:  8 wallclock secs ( 7.75 usr +  0.26 sys =  8.01
CPU)

But if I put:
  timethese
    (500_000, {
           multi_print_unbuffered  => sub {local $| = 1; multi_print() ;},
           multi_print_buffered    => sub {local $| = 0; multi_print() ;}, 
           single_print_unbuffered => sub {local $| = 1; single_print();},
           single_print_buffered   => sub {local $| = 0; single_print();},
          });

Benchmark: timing 500000 iterations of multi_print_buffered,
multi_print_unbuffered, single_print_buffered, single_print_unbuffered...
multi_print_buffered: 18 wallclock secs (15.79 usr +  0.65 sys = 16.44
CPU)
multi_print_unbuffered: 18 wallclock secs (16.57 usr +  0.37 sys = 16.94
CPU)
single_print_buffered:  7 wallclock secs ( 7.23 usr +  0.12 sys =  7.35
CPU)
single_print_unbuffered:  9 wallclock secs ( 7.57 usr +  0.32 sys =  7.89
CPU)

There is no difference between buffered and not buffered, it seems that
the localizing of $| is what makes the difference. The multi vs single
still holds.

But you don't modify $| unless you want to unbuffer, so the first test
seems to be fair to me. What do you think?

Do you think I should test it under Apache (thru ab) since it might behave
differently under Apache? But I doubt I'd be able to measure this unless
the text would be huge to get the real feel of the numbers. You saw that
I'd to run 500_000 tests to get some real numbers...

_____________________________________________________________________
Stas Bekman              JAm_pH     --   Just Another mod_perl Hacker
http://stason.org/       mod_perl Guide  http://perl.apache.org/guide 
mailto:[EMAIL PROTECTED]   http://perl.org     http://stason.org/TULARC
http://singlesheaven.com http://perlmonth.com http://sourcegarden.org

Reply via email to