How good is the performance of binary I/O libraries in these two languages> I am contemplating about re-writing an ugly (yet very fast) C++ code that processes binary files of around 5-10GB using standard fread and fwrite functions. What slow-down factor should I expect for an optimized implementation in F# and Haskell?
EDIT:
here is the C implementation of counting zero-bytes (buffer allocated on heap).
#include <stdio.h>
#include <stdlib.h>
#define SIZE 32*1024
int main(int argc, char* argv[])
{
FILE *fp;
char *buf;
long i = 0, s = 0, l = 0;
fp = fopen(argv[1], "rb");
if (!fp) {
printf("Openning %s failed\n", argv[1]);
return -1;
}
buf = (char *) malloc(SIZE);
while (!feof(fp)) {
l = fread(buf, 1, SIZE, fp);
for (i = 0; i < l; ++i) {
if (buf[i] == 0) {
++s;
}
}
}
printf("%d\n", s);
fclose(fp);
free(buf);
return 0;
}
The results:
$ gcc -O3 -o ioc io.c
$ ghc --make -O3 -o iohs io.hs
Linking iohs ...
$ time ./ioc 2.bin
462741044
real 0m16.171s
user 0m11.755s
sys 0m4.413s
$ time ./iohs 2.bin
4757708340
real 0m16.879s
user 0m14.093s
sys 0m2.783s
$ ls -lh 2.bin
-rw-r--r-- 1 14G Jan 4 10:05 2.bin
I blogged about this here.