Which costs more while looping; assignment or an if-statement?

前端 未结 6 1243
不知归路
不知归路 2020-12-29 06:04

Consider the following 2 scenarios:

boolean b = false;
int i = 0;
while(i++ < 5) {
    b = true;
}

OR

boolean b = false;         


        
相关标签:
6条回答
  • 2020-12-29 06:24

    Have you tested this? Working on a Linux system, I put your first example in a file called LoopTestNoIf.java and your second in a file called LoopTestWithIf.java, wrapped a main function and class around each of them, compiled, and then ran with this bash script:

    #!/bin/bash
    function run_test {
      iter=0
      while [ $iter -lt 100 ]
      do
        java $1
        let iter=iter+1
      done
    }
    time run_test LoopTestNoIf
    time run_test LoopTestWithIf
    

    The results were:

    real    0m10.358s 
    user    0m4.349s 
    sys     0m1.159s 
    
    real    0m10.339s
    user    0m4.299s 
    sys     0m1.178s
    

    Showing that having the if makes it slight faster on my system.

    0 讨论(0)
  • 2020-12-29 06:34

    Relative to your barebones example (and perhaps your real application):

    boolean b = false;
    // .. other stuff, might change b
    int i = 0;
    // .. other stuff, might change i
    b |= i < 5;
    while(i++ < 5) {
        // .. stuff with i, possibly stuff with b, but no assignment to b
    }
    

    problem solved?

    But really - it's going to be a question of the cost of your test (generally more than just if (boolean)) and the cost of your assignment (generally more than just primitive = x). If the test/assignment is expensive or your loop is long enough or you have high enough performance demands, you might want to break it into two parts - but all of those criteria require that you test how things perform. Of course, if your requirements are more demanding (say, b can flip back and forth), you might require a more complex solution.

    0 讨论(0)
  • 2020-12-29 06:43

    Actually, this is the question I was interested in… (I hoped that I’ll find the answer somewhere to avoid own testing. Well, I didn’t…)

    To be sure that your (mine) test is valid, you (I) have to do enough iterations to get enough data. Each iteration must be “long” enough (I mean the time scale) to show the true difference. I’ve found out that even one billion iterations are not enough to fit to time interval that would be long enough… So I wrote this test:

    for (int k = 0; k < 1000; ++k)
    {
        {
            long stopwatch = System.nanoTime();
    
            boolean b = false;
            int i = 0, j = 0;
            while (i++ < 1000000)
                while (j++ < 1000000)
                {
                    int a = i * j; // to slow down a bit
                    b = true;
                    a /= 2; // to slow down a bit more
                }
    
            long time = System.nanoTime() - stopwatch;
            System.out.println("\\tasgn\t" + time);
        }
        {
            long stopwatch = System.nanoTime();
    
            boolean b = false;
            int i = 0, j = 0;
            while (i++ < 1000000)
                while (j++ < 1000000)
                {
                    int a = i * j; // the same thing as above
                    if (!b)
                    {
                        b = true;
                    }
                    a /= 2;
                }
    
            long time = System.nanoTime() - stopwatch;
            System.out.println("\\tif\t" + time);
        }
    }
    

    I ran the test three times storing the data in Excel, then I swapped the first (‘asgn’) and second (‘if’) case and ran it three times again… And the result? Four times “won” the ‘if’ case and two times the ‘asgn’ appeared to be the better case. This shows how sensitive the execution might be. But in general, I hope that this has also proven that the ‘if’ case is better choice.

    Thanks, anyway…

    0 讨论(0)
  • 2020-12-29 06:44

    Are you trying to find out if doing the assignment each loop is faster in total run time than doing a check each loop and only assigning once on satisfaction of the test condition?

    In the above example I would guess that the first is faster. You perform 5 assignments. In the latter you perform 5 test and then an assignment.

    But you'll need to up the iteration count and throw in some stopwatch timers to know for sure.

    0 讨论(0)
  • 2020-12-29 06:44

    Any compiler (except, perhaps, in debug) will optimize both these statements to

    bool b = true;
    

    But generally, relative speed of assignment and branch depend on processor architecture, and not on compiler. A modern, super-scalar processor perform horribly on branches. A simple micro-controller uses roughly the same number of cycles per any instruction.

    0 讨论(0)
  • 2020-12-29 06:48

    Please do not forget the rules of Optimization Club.

    1. The first rule of Optimization Club is, you do not Optimize.
    2. The second rule of Optimization Club is, you do not Optimize without measuring.
    3. If your app is running faster than the underlying transport protocol, the optimization is over.
    4. One factor at a time.
    5. No marketroids, no marketroid schedules.
    6. Testing will go on as long as it has to.
    7. If this is your first night at Optimization Club, you have to write a test case.

    It seems that you have broken rule 2. You have no measurement. If you really want to know, you'll answer the question yourself by setting up a test that runs scenario A against scenario B and finds the answer. There are so many differences between different environments, we can't answer.

    0 讨论(0)
提交回复
热议问题