octobre 2010

PERL  Who’s the boss?

Do you remember the old post where I compared the performance between different PERL modules for basic statistic? In the former, the winner was PDL. But, this time, for particular reasons, I’ve experimented to put some C code into a PERL program by using the Inline::C module. Again, the principle is quite simple: compute Pearson coefficient. Benchmark results are quite… impressive :

Benchmark: running C, bas, pdl for at least 5 CPU seconds...
         C:  5 wallclock secs ( 5.25 usr +  0.01 sys =  5.26 CPU) @ 99364.26/s (n=522656)
       bas:  6 wallclock secs ( 5.24 usr +  0.01 sys =  5.25 CPU) @ 3256.19/s (n=17095)
       pdl:  5 wallclock secs ( 5.30 usr +  0.01 sys =  5.31 CPU) @ 3855.93/s (n=20475)
       Rate   bas   pdl     C
bas  3256/s    --  -16%  -97%
pdl  3856/s   18%    --  -96%
C   99364/s 2952% 2477%    --

Ouch ! Say no more… The source code could be find here (be polite, this is my first C code into a PERL program ;-) !)…

PERL  Flush file output

If you want to write something to a file (i.e write to a log file), PERL will handle a buffer and won’t write instantly to the file. In most case, this behaviour is the best (since writing to a file will downgrade performance of your program). But, it is not what you will expect when you want to write to a logfile (as your program may stop, leaving the file empty). So, in this case, you should use binmode to give your filehandle a unix layer, which is unbuffered :

open (LOG, ">".$filename) || die "cannot create file\n";
binmode (LOG, ":unix");

Then, your PERL program will write instantly to your logfile…