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...