如何准确计算Java程序写入或读取文件所需的时间?
如何准确计算 Java 程序向文件写入或读取多个字节所需的时间?
准确测量时间非常重要。 (时间应该由程序本身计算)。
How to compute accurately the time it takes a Java program to write or read a number of bytes from/to a file ?
It is really important that the time is being measured accurately. (The time should be computed by the program itself).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
标准的习语是:
The standard idiom is:
未经测试,但类似:
not tested, but something like:
这里有一个代码示例:
http://www.goldb.org/stopwatchjava.html
There is a code sample here:
http://www.goldb.org/stopwatchjava.html
我这样做的方法就是循环运行它几次。 就像如果你运行它 1000 次并对其进行计时,就会得到毫秒数。 运行它 1,000,000 次,它会给出微秒。
如果您还想找出为什么它花了那么长时间,您可以在它运行时暂停它几次(例如 10 次),这会告诉您它在做什么以及为什么。
The way I would do that is just run it in a loop some number of times. Like if you run it 1000 times and clock it, that gives you milliseconds. Run it 1,000,000 times, and it gives you microseconds.
If you also want to find out why it's taking as long as it is, you can just pause it some number of times (like 10) while it's running, and that will tell you what it's doing and why.
get System.xxx 方法的问题在于该方法本身需要几毫秒的时间来计算。 通常“接受”的方法是运行测试数万次并计算平均值。
另外,根据您的操作系统,有一种称为时间粒度的东西(以 Windows 为例)。 这是操作系统可以计算的最短时间。 在某些操作系统上它是毫秒,在其他操作系统上它是纳秒。 它可能与您的情况相关,也可能不相关。
The problem with the get System.xxx method is that the method itself needs a few milliseconds to compute. The usually "accepted" way of doing it is running the test a few tens of thousands of times and calculating an average of this.
Also, depending on your OS there is something called the time granularity (example for windows). This is the smallest amount of time your OS can compute. On some OS its a millisecond, on some others its a nanosecond. It might or might not be relevant in your case.