¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita0
Which one is more efficient in terms of memory usage and instruction cycle if or for or switch
please need
2 Respuestas
+ 3
Between an if and a switch the switch probably wins.
I'm gonna ignore the for cause it's unrelated to these 2.
A switch uses a jump table or a binary search depending on the cases.
Cases like 0, 1, 2, 3 usually uses a jump table while if the cases are more random it'll use a binary search method.
An if/if else on the other hand checks every case 1 by 1 from top to bottom in both cases. Even with -O3. At least in my test cases.
0
Depends purpose of use.
If - in case single condition check.
Switch - in case you depend upon variable which takes multiple values.
For - if you want run set of instructions for certain number of times....
Hope this helps...!!!