Java任务运行时
首先我必须承认这些都是非常基本和原始的问题...我想演示Java中用于排序和搜索的不同算法,并获取运行时的值。 有些问题我无法解决:
有热点编译 - 这是我需要停用的运行时优化(我猜)。
如何获取运行时的时间值(秒)? 在执行之前启动计时器并在执行之后停止它......似乎有点原始。 而且计时器对象本身会消耗运行时...我需要避免这种情况。
Java API 中有什么可以用来解决这些问题吗?
谢谢, 克劳斯
First of all I have to admit that these are very basic and primitive questions... I want to demonstrate different Algorithms in Java for sorting and searching, and to get a value for the runtime. There're issues I cannot solve:
there's Hotspot compiling - which is a runtime optimization I need to deactivate (I guess).
How do I get time-values (seconds) for runtimes? Starting a timer before the execution and stopping it afterwards... seems a little primitive. And the timer-object itself consumes runtime... I need to avoid that.
Is there anything in the Java API one could utilize to solve these problems?
Thanks,
Klaus
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在命令行上使用
-Xint
禁用 HotSpot,以使性能降低一个数量级。 但是,您为什么不想衡量现实世界的性能呢? 编译时,不同的东西可能会成为瓶颈。通常对于微基准测试:
You can disable HotSpot with
-Xint
on the command line, to get an order of magnitude decrease in performance. However, why don't you want to measure real world performance? Different things can become bottlenecks when you compile.Generally for microbenchmarks:
System.nanoTime
to get a time measurement at the start and end使用
-Xint
JVM标志。 其他选项可以在此处查看。使用
ThreadMXBean
用于获取线程的 CPU/用户时间的 API。 可以在此处查看示例。Use
-Xint
JVM flag. Other options can be seen here.Use the
ThreadMXBean
API to get CPU/User times for your thread. An example can be seen here.使用 System.nanoTime() 两次消耗的时间不到 1 微秒。 我建议您运行任何基准测试几秒并取平均值,这样微秒的错误就不会很严重。
总的来说,我建议不要让事情变得比你需要的更复杂。
为了进行内置热身,我经常忽略前 10%-20% 的迭代。 就像是
Using System.nanoTime() twice consumes less than 1 micro-second. I suggest you run any benchmark for a number of second and take an average so a micro-second error won't be significant.
Overall, I would suggest not making things more complicated than you need it to be.
To have a built in warm up I often ignore the first 10%-20% of iterations. Something like