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?
Clearly fopen() is the cause of this 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
Do you use a Virus Scanner? If yes disable it first!
And some API Calls are slower on windows. E.G. your C:\ will be translated to /harddrive/something first (just an example).
There is a lot more than just the API being used here.
You are opening a file on the file system.
So the type of file system being used will affect time, as well as the hard ware speeds of the devices implementing the file system. There are just too many factors that you are not taking into account that you can accurately say X is the culprit for the slow speeds.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With