lundi 23 décembre 2019

Why single for-loop with multiple if-statements is slower than several separate statements with loops inside?

Why single for-loop with multiple if-statements is slower than several separate statements with loops inside?

Basically that is the case. I have single massive range and why iterating it once is slower than multiple iretations up to 2 times?

What I have:

for i in range(100000):
 if a:
  x[i]= ...
 if b:
  x[i]= ...
 if c:
  x[i]= ...

etc.

is slower than

if a:
 for i in range(100000):
  x[i]= ...

if b:
 for i in range(100000):
  x[i]= ...

if c:
 for i in range(100000):
  x[i]= ...

etc.

Aucun commentaire:

Enregistrer un commentaire