Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance of fopen() on win32

Tags:

c

linux

winapi

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?

like image 219
vrrathod Avatar asked Jul 20 '10 23:07

vrrathod


3 Answers

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

like image 181
Martin Beckett Avatar answered Nov 10 '22 01:11

Martin Beckett


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).

like image 4
Andreas Rehm Avatar answered Nov 10 '22 01:11

Andreas Rehm


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.

like image 2
Martin York Avatar answered Nov 10 '22 01:11

Martin York