Home > Net >  When is it worth using loop over if-else statements?
When is it worth using loop over if-else statements?

Time:01-06

Assume that I am looking for the index of some_array where some_array is equal to target. I know that python has list comprehension and np.where(), both of which would operate well for my purposes. But also assume that I want to do it with if-elif-else statements or with a for loop. The implementations if the length of the array is 3 would look like this:

if some_array[0]==target:
    return 0
elif some_array[1]==target:
    return 1
else:
    return 2 
for i in range(3):
    if some_array[i]==target:
        return i

So, when is it better to use a for loop over if-elif-else statement? I am mostly interested in the applications of it in python and in C, i.e., switch-cases.

My subquestions would be:

  • Do the compilers (or in Python's case, numba or cython) switch from a for loop to switch-cases or vice versa if it feels like the other approach is faster?
  • Is there a generally accepted good-coding practice that suggests a maximum length for an if-elif-else statements for better readability?
  • Is there a threshold for the length of the array or the number of iterations where one of them performs better than the other?

I apologise if this is asked before. I tried to check suggested questions but there were not helpful for my purposes.

Thanks in advance!

CodePudding user response:

The performance of an if is not going to matter. A for loop would be better than a lot of ifs for readability.

In this specific case, just use a builtin, which is generally faster.

return some_array.index(target)

If it's possible that target is not in the list

try:
    return some_array.index(target)
except ValueError:
    return -1

CodePudding user response:

So, when is it better to use a for loop over if-elif-else statement?

A loop is always clearer than an if-else if-else chain for this particular purpose. That is sufficient reason to prefer the loop, except possibly in the highly unlikely case that you trace a performance bottleneck to such a loop and find that it is relieved by changing to an if.

  • Do the compilers (or in Python's case, numba or cython) switch from a for loop to switch-cases or vice versa if it feels like the other approach is faster?

Loop unrolling is a standard optimization that many C compilers will perform when they think it will yield an improvement. A short loop might be unrolled completely out of existence, which is effectively the transformation you ask about.

I am not aware of compilers performing the reverse transformation.

  • Is there a generally expected good coding practice that suggests a maximum length for an if-elif-else statements to ensure ease of following the code?

Not as such.

Write clear code, and do not repeat yourself.

  • Is there a threshold for the length of the array or the number of iterations where one of them performs better than the other?

Not in particular. Performance has many factors, and few clear rules.

In general, first make it work, then, if necessary, make it fast.

  • Related