【发布时间】:2021-12-29 03:34:34
【问题描述】:
如果我想像日志功能一样频繁写入文件,哪种方式更好?
- 当我需要写入时,打开 iostream 并在完成后关闭
void log(const string& s){
iostream ios(log_path);
ios << s;
ios.close();
}
void main(){
while (need_to_log) {
log(some_string);
}
}
- 开始时将文件描述符保存为某个全局变量,在需要写入时使用它,在程序关闭时关闭。
iostream ios(log_path);
void log(const string& s){
ios << s;
}
void main(){
while (need_to_log) {
log(some_string);
}
ios.close();
}
这两者之间会有显着的性能差异吗?
【问题讨论】:
-
Will there be a significant performance difference取决于您认为“重要”的内容,也取决于使用的操作系统。 -
没有
iostream::close()