0

Which one is more efficient in terms of memory usage and instruction cycle if or for or switch

please need

9th Apr 2019, 12:13 PM
Durgadevi
Durgadevi - avatar
2 Respostas
+ 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.
9th Apr 2019, 1:05 PM
Dennis
Dennis - avatar
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...!!!
9th Apr 2019, 1:10 PM
Kuri
Kuri - avatar