fopen:是好主意离开,还是使用缓冲区? [英] fopen: is it good idea to leave open, or use buffer?

查看:126
本文介绍了fopen:是好主意离开,还是使用缓冲区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有很多日志文件,我需要写入。它们在程序开始时创建,并在程序关闭时保存到文件。



我想知道在程序启动时是否更好:



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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆