fast file i/o in c
hello, i am currently writing a time critical c program and would like to know if there is a fast way of reading large text files. the text is millions of lines of sets of 3 integers representing a sparse matrix. thanks very much
|
call stat to get file size, allocate a buffer and call something like this:
Code:
/* read nbyte from a file - read whole file */ |
thanks, thats really good. if anyone else has any code it too would be much appreciated. cheers
|
mmap() might give you a small gain.
If you can, you probably want to do some of your mathematical computations in one thread *while* another thread reads data from the file. Of course, the best way to speed up disk IO is to change your hardware: Use faster disks or raid with striping or both.. But then, if the files aren't that large (only a couple million lines), they'll fit in RAM so you're mostly trying to minimize in-memory copies, not improve disk I/O. mmap() ought to help there. And the bottom line is, if you haven't profiled the code, do so before you choose places to optimize! Could be that your math takes 98% of the time and I/O is a moot point. |
aluser is correct. profile first, optimize later. Frequently, one function with a particularly poor algorithm can use 40% of elapsed time of the whole process.
|
All times are GMT -5. The time now is 02:53 AM. |