需要多长时间 c++打开一个文件
我试图确定我的代码运行速度比我想象的慢大约四倍的原因。
在我的代码中,我循环迭代 100000 次。每 500 个循环,我将一行打印到文件中(以附加模式打开它,打印该行并关闭它)。在一台像样的计算机(HPC 集群)上,这是否有可能将 12 小时的运行变成 48 小时的运行?
希望我的问题很清楚。
I'm trying to determine the reason why my code runs roughly four times slower than I thought it would.
In my code I'm iterating over a loop 100000 times. Once every 500 loops I print a line into a file (opening it in append mode, printing the line and closing it). Is it possible that on a decent computer (HPC cluster) this turns a 12h run into 48h run?
Hope my question is clear.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试运行 A/B 测试:
在测试 A 中,您按照描述进行日志记录 - 打开和关闭每条日志消息的文件。
在测试 B 中,尝试从一开始就打开文件,在整个运行过程中保持打开状态,并且仅在程序退出时才将其关闭。
打开和关闭文件所需的时间取决于您的操作系统、C IO 库实现和存储子系统。我怀疑测试 B 会更快,但你需要在自己的软件和硬件上运行一些实验才能确定。
Try running an A/B test:
In test A, you do the logging as you describe - open and close the file for each log message.
In test B, try opening the file at the very beginning, keeping it open throughout the run, and only closing it when your program exits.
The time it takes to open and close files will depend on your operating system, C IO library implementation and your storage subsystem. I suspect that test B will be faster, but you need to run some experiments on your own software and hardware to be sure.
只需暂停几次,然后查看堆栈。
无论是什么导致它花费 4 倍的时间,都会出现在大约四分之三的堆栈样本中。
你不能错过它。
问题可能是打开/关闭文件,
但我怀疑如果你只做了200次。
无论如何,没有必要去假设。
只需暂停几次即可确定问题所在。
Just pause it a few times, and look at the stack.
Whatever is making it take 4 times as long will appear on roughly 3 out of 4 stack samples.
You can't miss it.
It's possible the problem is opening/closing files,
but I doubt it if you're only doing it 200 times.
Anyway, there's no need to hypothesize.
Just pause it a few times and find out for certain what the problem is.