English 中文(简体)
CPU 在 Java的执行时间
原标题:CPU execution time in Java

我想计算一下邮联履行我的职责在 Java执行的时间。 目前,我的工作如下。

   long startTime = System.currentTimeMillis();
    myfunction();
    long endTime = System.currentTimeMillis();
    long searchTime = endTime - startTime;

但我发现,对于同一个I/PI,视系统负荷不同,时间不同。

因此,我如何履行我的职责。

最佳回答

随着联合选举机制加快了所花费的时间,时间将有所不同。 你第二次这样做,总是比第一次快。 (第一次必须装上班级,用固定区块标示) 在你操作了10 000次方法之后,将再次加快速度(在设定的门槛值下,该门槛值将代码编成本地机器编码)。

为了获得微型基准的可再生平均时间,我建议你忽视头10 000英亩,然后将它管理到2至10秒。

e.g.

long start = 0;
int runs = 10000; // enough to run for 2-10 seconds.
for(int i=-10000;i<runs;i++) {
    if(i == 0) start = System.nanoTime();
    // do test
}
long time = System.nanoTime() - start;
System.out.printf("Each XXXXX took an average of %,d ns%n", time/runs);

非常重要: 这种方法只有一种。 这是因为它根据使用的方法选择了整个方法。 如果你有一个像这样繁忙的休息室,那么,由于他们没有运行,而且选择不好,以后的休息时间会比较缓慢。

问题回答
  1. System.currentTimeMillis() will only ever measure wall-clock time, never CPU time.
  2. If you need wall-clock time, then System.nanoTime() is often more precise (and never worse) than currentTimeMillis().
  3. ThreadMXBean.getThreadCPUTime() can help you find out how much CPU time a given thread has used. Use ManagementFactory.getThreadMXBean() to get a ThreadMXBean and Thread.getId() to find the id of the thread you re interested in. Note that this method need not be supported on every JVM!

一些简介员(Jprofile、Jprobe、Youkit)可以分析这些数据。 不仅如此,还有很多......(例如记忆利用、阅读细节等)





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签