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 | |
|
Re^2: perl printf vs C fprintf performance
by Leotrillo (Initiate) on Nov 02, 2009 at 21:58 UTC | |
by Anonymous Monk on Nov 03, 2009 at 04:32 UTC |