実行時間を測定するためのJavaのコマンドはありますか?
何かのようなもの
System.out.println(execution.time);
コードの最後に。
実行時間を測定するためのJavaのコマンドはありますか?
何かのようなもの
System.out.println(execution.time);
コードの最後に。
これを行う方法の完全で少し変更された例を次に示します。
public class ExecutionTimer {
private long start;
private long end;
public ExecutionTimer() {
reset();
start = System.currentTimeMillis();
}
public void end() {
end = System.currentTimeMillis();
}
public long duration(){
return (end-start);
}
public void reset() {
start = 0;
end = 0;
}
public static void main(String s[]) {
// simple example
ExecutionTimer t = new ExecutionTimer();
for (int i = 0; i < 80; i++){
System.out.print(".");
}
t.end();
System.out.println("\n" + t.duration() + " ms");
}
}
以下を使用して、自分で簡単に実装できますSystem.currentTimeMillis()
。
final long start = System.currentTimeMillis();
executeLongRunningTask();
final long durationInMilliseconds = System.currentTimeMillis()-start;
System.out.println("executeLongRunningTask() took " + durationInMilliseconds + "ms.");
あるいは (特に、タスクの実行時間がそれほど長くない場合) System.nanoTime()
、. 仕組みとは逆にcurrentTimeMillis()
、 によって返される値は、nanoTime()
指定された時間に関連していないことに注意してください。これは、時間範囲nanoTime()
を測定するためにのみ使用でき、特定の時点を識別するために使用できないことを意味します。
プロファイラーを実行するか、2 つの呼び出しの違いを使用してSystem.currentTimeMillis()
このような :
long start = System.currentTimeMillis();
....
doSomething();
....
long end = System.currentTimeMillis();
System.out.println("Execution time was "+(end-start)+" ms.");
最も簡単な方法は、コード実行の前後に System.currentTimeMillis() を使用することです。Joda-Time には、より洗練されたバージョンがあります: http://joda-time.sourceforge.net/
測定対象の詳細を知りたい場合は、JMX、特に ThreadMXBean を使用することを強くお勧めします: http://download.oracle.com/javase/6/docs/api/java/lang/management/ThreadMXBean.html
コードサンプル:
ThreadMXBean bean = ManagementFactory.getThreadMXBean( );
if (bean.isCurrentThreadCpuTimeSupported()) {
long cpuTime = bean.getCurrentThreadCpuTime( );
}
long userTime = bean.getCurrentThreadUserTime( );
コード サンプルを含む非常に完全な説明は、次の場所にあります 。
より詳細なタイミングについては、ThreadMXBean を使用します。
public class Timer {
static {
// needed to request 1ms timer interrupt period
// http://discuss.joelonsoftware.com/default.asp?joel.3.642646.9
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(Integer.MAX_VALUE); (Windows NT)
} catch (InterruptedException ignored) {
}
}
});
thread.setName("Timer");
thread.setDaemon(true);
thread.start();
}
private final ThreadMXBean threadMX = ManagementFactory.getThreadMXBean();
private final long elapsedStart;
private final long cpuStart;
private final long userStart;
public Timer() {
cpuStart = threadMX.getCurrentThreadCpuTime();
userStart = threadMX.getCurrentThreadUserTime();
elapsedStart = System.nanoTime();
}
public void times() {
long elapsed = elapsedStart - System.nanoTime();
long cpu = cpuStart - threadMX.getCurrentThreadCpuTime();
long user = userStart - threadMX.getCurrentThreadUserTime();
System.out.printf("elapsed=%-8.3f cpu=%-8.3f user=%-8.3f [seconds]",
elapsed/1.0e9, cpu/1.0e9, user/1.0e9);
}
}
time
実行するアクションをパラメーターとして取り、その実行に必要な時間を測定して出力するコントロールの抽象化を設計できます。
コード:
interface Action<A> {
public A perform();
}
class Timer {
public static <A> A time(final String description, final Action<A> action) {
final long start = System.nanoTime();
final A result = action.perform();
final long end = System.nanoTime();
System.out.println(description + " - Time elapsed: " + (end - start) +"ns");
return result;
}
}
class Main {
public static void main(final String[] args) {
final int factorialOf5 = Timer.time("Calculating factorial of 5",
new Action<Integer>() {
public Integer perform() {
int result = 1;
for(int i = 2; i <= 5; i++) {
result *= i;
}
return result;
}
}
);
System.out.println("Result: " + factorialOf5);
}
}
// Output:
// Calculating factorial of 5 - Time elapsed: 782052ns
// Result: 120
RoflcoptrException のクラス例が気に入りました。私はそれを本質的に書き直しました:
public class ExecutionTimer {
private long start;
public ExecutionTimer() {
restart();
}
public void restart() {
start = System.currentTimeMillis();
}
public long time(){
long end = System.currentTimeMillis();
return (end-start);
}
public String toString() {
return "Time="+time()+" ms";
}
}