|
Para medir minimamente a diferença entre C e Perl, foram utilizados
dois pequenos programas em ambas as linguagens que copiam um ficheiro
substituindo todos os "0" por "A". Em seguida foi feito um "time" a 500
chamadas de cada um dos programas. Abaixo estão os resultados.
Versão em C
real 0m11.76s
user 0m4.28s
sys 0m6.40s
Versão Perl
real 1m10.83s
user 0m55.53s
sys 0m13.32s
Programas utilizados
x.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define BUFFERSIZE 8192
int main() {
int infile, outfile, i;
ssize_t bytesread;
char buffer[ BUFFERSIZE ];
if( ( infile = open( "test.in", O_RDONLY ) ) == -1 ) {
perror( "test.in" );
return 1;
}
if( ( outfile = open( "test.out", O_CREAT | O_WRONLY ) ) == -1 ) {
perror( "test.out" );
return 0;
}
while( ( bytesread = read( infile, ( void * ) buffer, BUFFERSIZE ) ) > 0 ) {
for( i = 0; i < bytesread; i++ )
if( buffer[ i ] == '0' )
buffer[ i ] = 'A';
write( outfile, ( void * ) buffer, bytesread );
}
close( outfile );
close( infile );
return 0;
}
x.pl
#!/bin/perl
open (INFILE, "test.in");
open (OUTFILE, ">test.out" );
while ($inputline = <INFILE>) {
if ( $inputline =~ /0/i ) {
$inputline =~ s:0:A:g;
}
print OUTFILE $inputline;
}
close( INFILE );
close( OUTFILE );
multiple-c.sh
#!/bin/ksh
COUNT=1
while [ $COUNT -le 100 ]; do
./x
COUNT=$(($COUNT+1))
done
multiple-pl.sh
#!/bin/ksh
COUNT=1
while [ $COUNT -le 100 ]; do
./x.pl
COUNT=$(($COUNT+1))
done
time.sh
#!/bin/ksh
echo C VERSION
time ./multiple-c.sh
echo ""
echo PERL VERSION
time ./multiple-pl.sh
test.in
0123456789 0123456789 0123456789 0123456789 0123456789 0123456789 0123456789 0123456789 0123456789 0123456789
0123456789 0123456789 0123456789 0123456789 0123456789 0123456789 0123456789 0123456789 0123456789 0123456789
...
|