获取另一个方法的运行时的方法

huangapple go评论68阅读模式
英文:

a method to get the runtime of another method

问题

我知道如何从这里获取方法的运行时
https://stackoverflow.com/questions/180158/how-do-i-time-a-methods-execution-in-java

现在我需要多次获取运行时间,是否有一种方式可以像这样做:

public long exTime(methodToTime()) 
    long startTime = System.nanoTime(); 
    methodToTime(); 
    long endTime = System.nanoTime(); 

    long duration = (endTime - startTime);

感谢您的帮助

编辑:为了更具体,我当前的代码是

System.out.println("1001*00*10");
long startTime0 = System.nanoTime(); 
revealStr("1001*00*10"); 
long endTime0 = System.nanoTime(); 
long duration0 = (endTime0 - startTime0); 
System.out.println("The runtime is " + endTime0);

System.out.println("10**01*1*0");
long startTime1 = System.nanoTime(); 
revealStr("10**01*1*0"); 
long endTime1 = System.nanoTime(); 
long duration1 = (endTime0 - startTime1); 
System.out.println("The runtime is " + endTime1);

System.out.println("0*1*0**0**");
long startTime2 = System.nanoTime(); 
revealStr("0*1*0**0**"); 
long endTime2 = System.nanoTime(); 
long duration2 = (endTime2 - startTime2); 
System.out.println("The runtime is " + endTime2);

System.out.println("****1*1***");
long startTime3 = System.nanoTime(); 
revealStr("****1*1***"); 
long endTime3 = System.nanoTime(); 
long duration3 = (endTime3 - startTime3); 
System.out.println("The runtime is " + endTime3);

System.out.println("**********");
long startTime4 = System.nanoTime(); 
revealStr("**********"); 
long endTime4 = System.nanoTime(); 
long duration4 = (endTime4 - startTime4); 
System.out.println("The runtime is " + endTime0);

这是重复且冗余的。

英文:

I know how to get the runtime of a method from here
https://stackoverflow.com/questions/180158/how-do-i-time-a-methods-execution-in-java

Now I need to get the run time multiple times, so is there a way to make something like

public long exTime(methodToTime())
long startTime = System.nanoTime();
methodToTime();
long endTime = System.nanoTime();
long duration = (endTime - startTime);

Thanks for your help

edit: to be more specific, my current code is

		long startTime0 = System.nanoTime();
revealStr("1001*00*10");
long endTime0 = System.nanoTime();
long duration0 = (endTime0 - startTime0);
System.out.println("The runtime is " + endTime0);
System.out.println("10**01*1*0");
long startTime1 = System.nanoTime();	
revealStr("10**01*1*0");
long endTime1 = System.nanoTime();
long duration1 = (endTime0 - startTime1);
System.out.println("The runtime is " + endTime1);
System.out.println("0*1*0**0**");
long startTime2 = System.nanoTime();
revealStr("0*1*0**0**");
long endTime2 = System.nanoTime();
long duration2 = (endTime2 - startTime2);
System.out.println("The runtime is " + endTime2);
System.out.println("****1*1***");
long startTime3 = System.nanoTime();
revealStr("****1*1***");
long endTime3 = System.nanoTime();
long duration3 = (endTime3 - startTime3);
System.out.println("The runtime is " + endTime3);
System.out.println("**********");
long startTime4 = System.nanoTime();
revealStr("**********");
long endTime4 = System.nanoTime();
long duration4 = (endTime4 - startTime4);
System.out.println("The runtime is " + endTime0);

which is repetitive and redundant

答案1

得分: 1

这是一个我用来比较两种方法性能的实用方法。

public static void testPerformance(long loopTime, Runnable r1, Runnable r2){
    long startTime;
    long endTime;
    startTime = System.currentTimeMillis();
    for (int i = 0; i < loopTime; i++) {
        r1.run();
    }
    endTime = System.currentTimeMillis();
    System.out.printf("循环 %d 次,总耗时 %d 秒,每次耗时 %f 毫秒\n", loopTime, (endTime - startTime) / 1000, (double) (endTime - startTime) / loopTime);

    startTime = System.currentTimeMillis();
    for (int i = 0; i < loopTime; i++) {
        r2.run();
    }
    endTime = System.currentTimeMillis();
    System.out.printf("循环 %d 次,总耗时 %d 秒,每次耗时 %f 毫秒\n", loopTime, (endTime - startTime) / 1000, (double) (endTime - startTime) / loopTime);
}

你可以像这样使用:

PerformanceUtils.testPerformance(loopTime, () -> {
    // 做一些事情
}, () -> {
    // 做一些事情
});
英文:

This is a util method I used to compare the performance of two method.

public static void testPerformance(long loopTime, Runnable r1,Runnable r2){
long startTime;
long endTime;
startTime=System.currentTimeMillis();
for (int i = 0; i &lt; loopTime; i++) {
r1.run();
}
endTime=System.currentTimeMillis();
System.out.printf(&quot;loop %d times, total spend %d s, each spend %f ms\n&quot;,loopTime,(endTime-startTime)/1000,(double)(endTime-startTime)/loopTime);
startTime=System.currentTimeMillis();
for (int i = 0; i &lt; loopTime; i++) {
r2.run();
}
endTime=System.currentTimeMillis();
System.out.printf(&quot;loop %d times, total spend %d s, each spend %f ms\n&quot;,loopTime,(endTime-startTime)/1000,(double)(endTime-startTime)/loopTime);
}

You can use like this:

PerformanceUtils.testPerformance(loopTime,()-&gt;{
//do some thing
},()-&gt;{
//do some thing
});

huangapple
  • 本文由 发表于 2020年9月29日 06:09:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/64110267.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定