tags:

views:

206

answers:

4

Hello,

I'm writing several C programs for an embedded system where every bit of performance we can squeeze out will matter. Part of that is accessing log files. When determining if a file exists, is there any performance difference between using open / fopen, and stat ? I've been using stat on the assumption that it only has to do a quick check against the file system, whereas fopen would have to actually gain access to a file and manipulate internal data structures before returning. Is there any merit to this ?

+2  A: 

stat is probably better, since it doesn't have to allocate resources for actually reading the file. You won't have to call fclose to release those resources, and you may also benefit from caching of recently checked files.

When it doubt, test it out. Time a big loop that checks for 1000 files using each method, with the appropriate mix of filenames that exist and don't exist.

If you have the source code for stat and fopen, you should be able to read through it and get an idea as to which will require more resources.

tomlogic
stat actually had 3X better performance for checking file existence than fopen on our embedded boxes, I was kind of surprised. Thanks for the suggestion.
Alex Marshall
A: 

stat() does not not to create any user-side memory data structures. No matter how aggressive your caching policy, stat will not try pre-read the file's data. I think stat() is a safer bet.

How about access()?

Arkadiy
+1  A: 

If you want to squeeze out performance with respect to querying file existence and opening files, minimize the number of fopen and stat calls in general. The call to the file system should be way more expensive than anything the runtime does to translate it.

MSN
A: 

For only testing file existence, stat() would be preferred over fopen().

However, depending upon your setup, it could be worthwhile to use lstat() instead of stat().

Sparky