in reply to perl printf vs C fprintf performance

I used the following code to benchmark, in an attempt to reduce the comparison solely to the actual printf calls:

use warnings; use Benchmark qw(cmpthese); open OUTF, '>', '/dev/null' or die "/dev/null: $!\n"; cmpthese(0, { perlprint => q{ for my $j (0 .. 255) { printf OUTF "host DAAA%02X { blah blah XX:XX:XX:%02X; }\n", $j, +$j; } }, Cprint => q{ for my $j (0 .. 255) { C_print(*OUTF, $j); } }, }); use Inline 'C' => <<INLINE; void C_print(FILE *fp, int j) { fprintf(fp, "host DAAA%02X { blah blah XX:XX:XX:%02X; }\n", j, j); } INLINE

This consistently reported the C code as being about twice as fast as the perl code (on my perl-5.8.5 installation), which seems about reasonable to me given all the extra layers of indirection the perl code has to wade through.

One of the differences between perl-5.6.x and perl-5.8.x is that the 'perlio' abstraction is now the default, and with perlio enabled we get to take advantage of some of the lower-level I/O mechanisms available to the C library fprintf. It is possible that switching to a perl built with 'perlio' will get you a factor closer to this - check for "perlio=define" in the perl -V output.

Hugo

Replies are listed 'Best First'.
Re^2: perl printf vs C fprintf performance
by decnartne (Beadle) on Mar 15, 2005 at 14:23 UTC
    ...thx for the explanation

    decnartne ~ entranced

Re^2: perl printf vs C fprintf performance
by Leotrillo (Initiate) on Nov 02, 2009 at 21:58 UTC
    Hi, my name is Leo an i have the same problem, is very slow to open some document to write in it. you comment "It is possible that switching to a perl built with 'perlio' will get you a factor closer to this - check for "perlio=define" in the perl -V output." Can you put some examples to see how i can to accelerate this r/w docs in perl? thank you.
      useperlio is an option you set when compiling/installing perl http://search.cpan.org/dist/perl/INSTALL
      $ perl -V |grep -i perlio useperlio=define, d_sfio=undef, uselargefiles=define, usesocks=und +ef USE_LARGE_FILES USE_PERLIO USE_SITECUSTOMIZE $ perl -V:useperlio useperlio='define';