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
In reply to Re: perl printf vs C fprintf performance
by hv
in thread perl printf vs C fprintf performance
by decnartne
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |