如果我测量执行一段代码所需的时间就意味着我知道该代码的效率如何?爪哇
给出以下代码段,哪一段代码效率更高?真正的方法returnSomething()实际上也可以返回0,所以需要try/catch。
//piece one
long sleepTime = 200;
try{ sleepTime /= returnSomething();}
catch(Exception e){sleepTime = 200;}
private int returnSomething(){
return 1;
}
//or
//piece two
long sleepTime = 200;
if(returnSomething() == 3){sleepTime = 67;}
else if(returnSomething() == 2){sleepTime = 100;}
else if(returnSomething() == 1){sleepTime = 200;}
private int returnSomething(){
return 1;
}
我试图找出哪一段代码在处理器使用方面更有效,他们机器人做了同样的事情。我想知道我为测试编写的代码是否适合该目的,或者我是否可以对代码进行其他类型的测试。我的发现表明,尽管第 2 部分使用硬编码 if 语句并且始终执行最后一个 if 语句,但其效率提高了 9 倍(执行时间减少了 9 倍)。
完整的工作计划
public class CodePerformanceTester
{
public static void main(String[] args){
CodePerformanceTester tester = new CodePerformanceTester();
tester.start();
}
public void start(){
double start = System.currentTimeMillis();
long sleepTime = 200;
for(int i=0; i<10000000; i++){
//uncoment here the two lines below
//try{ sleepTime /= returnSomething();}
//catch(Exception e){sleepTime = 200;}
//coment the IF STATEMENTS when above code uncomented
if(returnSomething() == 3){sleepTime = 67;}
else if(returnSomething() == 2){sleepTime = 100;}
else if(returnSomething() == 1){sleepTime = 200;}
}
double end = System.currentTimeMillis();
System.out.println("Execution time for 10 million iteration was "+(end-start)+" ms.");
}
private int returnSomething(){
return 1;
}
}
Given the following pieces of code, which one is more efficient? The real method returnSomething() can also return 0 in reality so try/catch is needed.
//piece one
long sleepTime = 200;
try{ sleepTime /= returnSomething();}
catch(Exception e){sleepTime = 200;}
private int returnSomething(){
return 1;
}
//or
//piece two
long sleepTime = 200;
if(returnSomething() == 3){sleepTime = 67;}
else if(returnSomething() == 2){sleepTime = 100;}
else if(returnSomething() == 1){sleepTime = 200;}
private int returnSomething(){
return 1;
}
I was trying to figure out which piece of code is more efficient in terms of processor usage, they bot do the same thing. I would like to know if the code I wrote for testing is fit for the purpose or whether I can do other kind of testing on the code. My findings show that piece 2 is 9 times more efficient (9 times less time to execute) even though it uses hard-code if statements and the last if statement is always executed.
Full working program
public class CodePerformanceTester
{
public static void main(String[] args){
CodePerformanceTester tester = new CodePerformanceTester();
tester.start();
}
public void start(){
double start = System.currentTimeMillis();
long sleepTime = 200;
for(int i=0; i<10000000; i++){
//uncoment here the two lines below
//try{ sleepTime /= returnSomething();}
//catch(Exception e){sleepTime = 200;}
//coment the IF STATEMENTS when above code uncomented
if(returnSomething() == 3){sleepTime = 67;}
else if(returnSomething() == 2){sleepTime = 100;}
else if(returnSomething() == 1){sleepTime = 200;}
}
double end = System.currentTimeMillis();
System.out.println("Execution time for 10 million iteration was "+(end-start)+" ms.");
}
private int returnSomething(){
return 1;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第一部分可能会慢一些,因为除法比比较值更昂贵。
The first piece is probably slower because dividing is more expensive than comparing values.