in reply to Re^2: perl performance vs egrep
in thread perl performance vs egrep

print is just plain expensive. Here's a really quickly hacked together benchmark: $ perl -we'open my $zap, ">", "/dev/null"; use Benchmark "cmpthese"; cmpthese -10, { every => sub { for (1..100) { print $zap $_ } }, tens => sub { my $x; for (1..100) { $x .= $_; print $zap substr($x,0,1000,"") unless $x % 10 } } }'

Replies are listed 'Best First'.
Re^4: perl performance vs egrep
by demerphq (Chancellor) on Jan 25, 2005 at 17:56 UTC

    This must be OS dependent or something... (hard to say as you didnt post the output). On my system print with no cache is much faster:

    open my $zap, ">", "NULL:"; use Benchmark "cmpthese"; cmpthese -10, { every => sub { for (1..100) { print $zap $_ } }, tens => sub { my $x; for (1..100) { $x .= $_; unless ($x % 10) { print $zap $x; $x=""; } } print $zap $x if length $x; }, } __END__ # modfied code as posted above (5.6.1 on W2k) Rate tens every tens 1159/s -- -90% every 11285/s 874% -- # original code (5.6.1 on W2k) Rate tens every tens 1124/s -- -90% every 11219/s 899% -- # 5.8.4 (XP) Rate tens every tens 8305/s -- -73% every 30329/s 265% -- # 5.8.6 No Implicit Sys (XP) Rate tens every tens 9655/s -- -68% every 29755/s 208% --

    At least on Win32 it would seem just using print is much faster.... I guess this could be an example of what Steve Hay was talking about with Win32's realloc being crap and that it makes concatenation unecessarily slow. Update: No, that doesnt make sense, I just tried it with two different perl versions on two different win32 boxes and "every" won every one of them....

    Ignore this for now, its got a bug: "NUL" not "NULL:",as ysth pointed out. Ill redo it when i have access to all those perl versions again

    ---
    demerphq

      print is indeed fast when the open fails :) Trying with:
      use File::Spec; open my $zap, ">", File::Spec->devnull or die "open failed: $!"; use Benchmark "cmpthese"; cmpthese -10, { every => sub { for (1..100) { print $zap $_ } }, tens => sub { my $x; for (1..100) { $x .= $_; unless ($x % 10) { print $zap $x; $x=""; } } print $zap $x if length $x; }, }
      on first ActivePerl build 811, then cygwin perl 5.8.6 (the latter being a debugging build):
      $ aperl nullout.pl Rate every tens every 3792/s -- -34% tens 5728/s 51% -- $ perl nullout.pl Rate every tens every 1479/s -- -54% tens 3226/s 118% --