I have a program that need to write in a file every second.I thought there is a too much overhead if I open and close the file each time.So I decided to keep open the file io stream.But I don't want to rely on my unreliable hunch.How can I find the better way if I should keep file stream open or close by every additional writing?I want to check in CentOS environment.
How to know if io stream should stay open or close each time
io
Definitely keep it open! The only reason to close it would be to keep it synced, but you can call sync by hand if you are scared of power outages. There is huge overhead if you are closing it all the time, because the buffer has to be flushed, and there are more write calls if you are writing every single chunk by hand. Not to mention seeking to the end of the file, modifying the timestamps in filesystem and so on...