微基准输出高波动
我正在尝试对将二进制文件从一个位置保存到另一个位置所需的速度进行基准测试。
FileInputStream fis = new FileInputStream("/path/to/binary/file");
BufferedInputStream in = new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream("/path/to/save/new/binary/file");
BufferedOutputStream out = new BufferedOutputStream(fos);
long before = System.currentTimeMillis();
int data = 0;
while ((data = in.read()) != -1) {
out.write(data);
}
in.close();
out.close();
int seconds = (int) (System.currentTimeMillis() - before / 1000) % 60;
System.out.println("Took " + seconds);
无论是缓冲还是非缓冲,输出的时间范围为 3 到 64 ms。我本来期望更接近的范围,比如 40-50 或 10-20,或 30-40。造成如此高波动的原因是什么?
I'm trying to benchmark how fast or slow it would take to save a binary file from one location to another.
FileInputStream fis = new FileInputStream("/path/to/binary/file");
BufferedInputStream in = new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream("/path/to/save/new/binary/file");
BufferedOutputStream out = new BufferedOutputStream(fos);
long before = System.currentTimeMillis();
int data = 0;
while ((data = in.read()) != -1) {
out.write(data);
}
in.close();
out.close();
int seconds = (int) (System.currentTimeMillis() - before / 1000) % 60;
System.out.println("Took " + seconds);
Buffered or unbuffered, the output is anywhere from 3 to 64 ms. I would have expected a closer range, say 40-50 or 10-20, or 30-40. What's the cause of this high fluctuation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能通过仅运行单个测试来期望结果的一致性,特别是当它涉及系统调用和文件 I/O 时。
通过采取以下步骤来提高测量的意义。
You can't expect consistency in your results by running only a single test, especially if it involves system calls and file I/O.
Improve the meaningfulness of your measurements by taking the following steps.
文件 I/O 可能涉及很多“随机性”:
查看 cheeken 关于如何处理此问题的建议。
File I/O can have a lot of "randomness" involved:
Look at cheeken's suggestions for how to deal with this.