Alex Marshall
Alex Marshall

Reputation: 10312

Performance of fopen vs stat

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 ?

Upvotes: 3

Views: 5024

Answers (4)

Sparky
Sparky

Reputation: 14057

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

Upvotes: 0

MSN
MSN

Reputation: 54604

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.

Upvotes: 1

user3458
user3458

Reputation:

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()?

Upvotes: 4

tomlogic
tomlogic

Reputation: 11694

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.

Upvotes: 6

Related Questions