Java任务运行时

发布于 2024-07-19 00:55:03 字数 291 浏览 7 评论 0原文

首先我必须承认这些都是非常基本和原始的问题...我想演示Java中用于排序和搜索的不同算法,并获取运行时的值。 有些问题我无法解决:

  1. 有热点编译 - 这是我需要停用的运行时优化(我猜)。

  2. 如何获取运行时的时间值(秒)? 在执行之前启动计时器并在执行之后停止它......似乎有点原始。 而且计时器对象本身会消耗运行时...我需要避免这种情况。

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:

  1. there's Hotspot compiling - which is a runtime optimization I need to deactivate (I guess).

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我很OK 2024-07-26 00:55:03

您可以在命令行上使用 -Xint 禁用 HotSpot,以使性能降低一个数量级。 但是,您为什么不想衡量现实世界的性能呢? 编译时,不同的东西可能会成为瓶颈。

通常对于微基准测试:

  • 内获取开始和结束运行时的时间测量
  • 使用 System.nanoTime在合理的时间长度
  • 多次测量(有一些“热身”
  • )不同算法的交错测量
  • 不要在测量段中执行任何 I/O
  • 使用结果(HotSpot 可以完全优化掉琐碎的操作)
  • 在现实世界的情况下执行(或尽可能在 cloae 中执行)
  • 请记住双核是常态,更多核心将成为常态

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:

  • use System.nanoTime to get a time measurement at the start and end
  • run for a reasonable length of time
  • do the measurement a number of times over (there's some "warm up")
  • don't interleave measurements of different algorithms
  • don't do any I/O in the measured segment
  • use the result (HotSpot can completely optimise away trivial operations)
  • do it in a real world situation (or a cloae as possible)
  • remember dual core is the norm, and more cores will become normal
百合的盛世恋 2024-07-26 00:55:03
  1. 使用-Xint JVM标志。 其他选项可以在此处查看。

  2. 使用 ThreadMXBean 用于获取线程的 CPU/用户时间的 API。 可以在此处查看示例。

  1. Use -Xint JVM flag. Other options can be seen here.

  2. Use the ThreadMXBean API to get CPU/User times for your thread. An example can be seen here.

一笑百媚生 2024-07-26 00:55:03

使用 System.nanoTime() 两次消耗的时间不到 1 微秒。 我建议您运行任何基准测试几秒并取平均值,这样微秒的错误就不会很严重。

总的来说,我建议不要让事情变得比你需要的更复杂。

为了进行内置热身,我经常忽略前 10%-20% 的迭代。 就像是

long start;
int count;
for(int i = -count / 5; i < count; i++) {
    if (count == 0) start = System.nanoTime();
    // do tested code
}
long time = System.nanoTime() - start;
long average = time / count;
System.out.printf("Average time was %,d micro-seconds%n", average / 1000);

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

long start;
int count;
for(int i = -count / 5; i < count; i++) {
    if (count == 0) start = System.nanoTime();
    // do tested code
}
long time = System.nanoTime() - start;
long average = time / count;
System.out.printf("Average time was %,d micro-seconds%n", average / 1000);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文