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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.