To switch or if/else?
Gurus,
Which is the most efficient operation for a computer? To use a switch (case) structure or multiple if/else statements?
Best Regards,
Chris
No signature files!!! =)
February 19, 2001 10:27 PM
If your case statement is sparse, it will end up the same as multiple if-then-else statements anyway. If the case values are all close together (for example you switch on the values 0-9) and you have a decent compiler, it will probably rewrite it as a jump table with constant lookup time.
However, you should probably write whatever feels more comfortable or natural to you, and worry about efficiency later. Try profiling your code, you will probably discover it''s not spending much time on branches anyway.
Oh, and if you''re using C++, you can usually refactor your code such that most branches can be eliminated through polymorphism, and the question becomes moot ;-)
However, you should probably write whatever feels more comfortable or natural to you, and worry about efficiency later. Try profiling your code, you will probably discover it''s not spending much time on branches anyway.
Oh, and if you''re using C++, you can usually refactor your code such that most branches can be eliminated through polymorphism, and the question becomes moot ;-)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement