fopen:是好主意离开,还是使用缓冲区? [英] fopen: is it good idea to leave open, or use buffer?
问题描述
所以我有很多日志文件,我需要写入。它们在程序开始时创建,并在程序关闭时保存到文件。
我想知道在程序启动时是否更好:
fopen文件在程序结束时 - 我只是在需要时写入文件。
$ b p>我保存需要写入缓冲区,然后打开文件,从缓冲区写入,当程序结束时关闭文件。 $ p
好吧, fopen(3)+ fwrite(3) fclose(3)
是一个缓冲的I / O包,所以在它之上的另一层缓冲可能只是减慢了。
无论如何,去一个简单而正确的程序。如果它似乎运行缓慢,配置它,然后优化基于证据,而不是猜测。
So I have many log files that I need to write to. They are created when program begins, and they save to file when program closes.
I was wondering if it is better to do:
fopen() at start of program, then close the files when program ends - I would just write to the files when needed. Will anything (such as other file io) be slowed down with these files being still "open" ?
OR
I save what needs to be written into a buffer, and then open file, write from buffer, close file when program ends. I imagine this would be faster?
Well, fopen(3) + fwrite(3) + fclose(3)
is a buffered I/O package, so another layer of buffering on top of it might just slow things down.
In any case, go for a simple and correct program. If it seems to run slowly, profile it, and then optimize based on evidence and not guesses.
这篇关于fopen:是好主意离开,还是使用缓冲区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!