I am trying to write some code that works on both Linux and Win32. The most noticeable difference I find between them (in my code) is performance of fopen().
Following code takes 5 sec on my Ubuntu and the same code takes more than 100 sec on windows XP. I would like to make a note here that ubuntu is VM while XP is on a real machine.
time_t start = time(NULL);
for(int i=0; i < 100000; ++i){
FILE *fp = fopen("a.txt", "a");
if (fp != NULL)
{
fprintf(fp, "Hello World");
fclose(fp);
}
}
time_t end = time(NULL);
printf("\n It took %d seconds \n", end-start);
Clearly fopen() is the cause of this difference. I want to know why is it such a big difference?
No it’s more likely to be filesystem flushing.
On one system when you write, or more likely call fclose(), it blocks until the bytes are physically on disk (or at least until the disk says they are) – on the other the filesystems returns straight away, even if the flies are still being written