如果我测量执行一段代码所需的时间就意味着我知道该代码的效率如何?爪哇

发布于 2025-01-07 11:30:00 字数 1628 浏览 2 评论 0原文

给出以下代码段,哪一段代码效率更高?真正的方法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 技术交流群。

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

发布评论

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

评论(1

一杯敬自由 2025-01-14 11:30:00

第一部分可能会慢一些,因为除法比比较值更昂贵。

The first piece is probably slower because dividing is more expensive than comparing values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文