I'm writing several C programs for an embedded system where every bit of performance we can squeeze out will matter. P开发者_高级运维art 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 ?
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.
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()?
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.
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().
精彩评论