Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

Consider the following 2 scenarios:

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

OR

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

Which is more "costly" to do? If the answer depends on used language/compiler, please provide. My main programming language is Java.

Please do not ask questions like why would I want to do either.. They're just barebone examples that point out the relevant: should a variable be set the same value in a loop over and over again or should it be tested on every loop that it holds a value needed to change?

like image 729
heikkim Avatar asked Oct 28 '10 13:10

heikkim


People also ask

Is while loop expensive?

for loops and while loops also check a condition. In that sense, they also have a cost of 1 . The point is that there is typically something in the loop that will affect that condition. You'll notice both loops do the exact same thing and generate the exact same bytecode.

Is if-else expensive?

Sometimes a tiny amount of code costs a huge amount of performance. This is especially true of built-in language features, which many programmers assume to be extremely cheap if not free. Today we'll look at if and see just how much performance it can cost your app.

Which is faster if-else or for loop?

A WHILE statement loop will execute much faster than an IF statement loop in applications where the loop is placed many commands into a program. Consider, for example, a loop placed at the end of a very long program.

Can you put IF statements in for loop?

You can nest If statements inside For Loops. For example you can loop through a list to check if the elements meet certain conditions. You can also have a For Loop inside another For loop.


3 Answers

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.

like image 118
Andy Lester Avatar answered Nov 01 '22 11:11

Andy Lester


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.

like image 24
GreenMatt Avatar answered Nov 01 '22 11:11

GreenMatt


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.

like image 27
MattC Avatar answered Nov 01 '22 11:11

MattC