Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File size lookup in C

Tags:

c

unix

I was wondering if there was any significant performance increase in using sys/stat.h versus fseek() and and ftell()?

like image 804
catalystofanger Avatar asked May 06 '11 05:05

catalystofanger


1 Answers

Choosing between fstat() and the fseek()/ftell() combination, there isn't going to be much difference. The single function call should be slightly quicker than the double function call, but the difference won't be great.

Choosing between stat() and the combination isn't a very fair comparison. For the combination calls, the hard work was done when the file was opened, so the inode information is readily available. The stat() call has to parse the file path and then report what it finds. It should almost always be slower - unless you recently opened the file anyway so the kernel has most of the information cached. Even so, the pathname lookup required by stat() is likely to make it slower than the combination.

like image 172
Jonathan Leffler Avatar answered Nov 27 '22 12:11

Jonathan Leffler