How to Expeditiously Determine File Existence in Standard C
When confronted with the task of handling thousands of files, the need arises to swiftly ascertain their existence. This issue sparks the question: how can we effectively check for a file's presence in standard C implementations, including C 11, 14, 17, and C?
Seeking to address this concern, we present an empirical analysis comparing four distinct methods. Each method was subjected to 100,000 executions, half with existing files and half with non-existent files. The following results emerged from our meticulous testing:
Method | Time |
---|---|
exists_test0 (ifstream) | 0.485s |
exists_test1 (FILE fopen) | 0.302s |
exists_test2 (posix access()) | 0.202s |
exists_test3 (posix stat()) | 0.134s |
As evidenced by the data, the 'posix stat()' method reigns supreme as the fastest and most efficient approach for checking file existence. Its consistent sub-100ms execution time highlights its exceptional performance, making it the ideal choice for swiftly traversing large volumes of files.
The above is the detailed content of What's the Fastest Way to Check for File Existence in Standard C ?. For more information, please follow other related articles on the PHP Chinese website!