I'm probably missing something obvious... Why do many C fprintf statements take around 1 second (wallclock) and the equivalent perl printf statements take 4 seconds? Consider (ignoring for now some of my perhaps idiomatic failings):
#!/usr/bin/perl -w use strict; my $outf = "/tmp/outf.p.out"; open OUTF, ">$outf" or die "ERROR: could not open file $outf - $!\n"; foreach my $i ( 1..1000 ) { foreach my $j ( 0..255 ) { printf( OUTF "host DAAA%02X { blah blah XX:XX:XX:%02X; }\n", $ +j, $j ); printf( OUTF "host DAAA%02X { blah blah XX:XX:XX:%02X; }\n", $ +j, $j ); printf( OUTF "host DAAA%02X { blah blah XX:XX:XX:%02X; }\n", $ +j, $j ); printf( OUTF "host DAAA%02X { blah blah XX:XX:XX:%02X; }\n", $ +j, $j ); } } close OUTF; exit 0; __DATA__ /* C (somewhat?) equivalent of the perl script above */ #include <stdio.h> int main () { char *outf = "/tmp/outf.c.out"; FILE *fd; int i,j; if ( !( fd = fopen( outf, "w" ) ) ) { printf( "ERROR: could not open file %s\n", outf ); exit(1); } for ( i=1;i<=1000;i++ ) { for ( j=0;j<=255;j++ ) { fprintf( fd, " host DAAA%02X { blah blah XX:XX:XX:%02X; +}\n", j, j ); fprintf( fd, " host DAAA%02X { blah blah XX:XX:XX:%02X; +}\n", j, j ); fprintf( fd, " host DAAA%02X { blah blah XX:XX:XX:%02X; +}\n", j, j ); fprintf( fd, " host DAAA%02X { blah blah XX:XX:XX:%02X; +}\n", j, j ); } } fclose( fd ); exit(0); }
My configuration is as follows:
hostname% uname -a
SunOS hostname 5.9 Generic_117171-07 sun4u sparc SUNW,Sun-Fire-V440
hostname% perl -v

This is perl, v5.6.1 built for sun4-solaris-64int
Timed results are:
hostname% time ../bin/ftest
1.0u 0.0s 0:01 85% 0+0k 0+0io 0pf+0w
hostname% time ./ftest.pl
4.0u 0.0s 0:04 86% 0+0k 0+0io 0pf+0w
(... for the curious, I have ported a C program which generates our DHCP table to perl, thus the pseudo formatting of the string...)

Taking out the hex formatting results in a faster run time (duh!), but still slower than equivalent C fprintf().

Thanks in advance for any info...

decnartne ~ entranced


In reply to 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.