What is optimal algorithm for switch case
-
[quote author="Andre" date="1321960502"]
Is this a speed-critical part of your code, for instance? How important is code size for you, if you weigh it against maintainability and against speed? [/quote]
Speed is more important, maintainability is next and then size.
-
In that case: you should probably stick to using a switch, or something that results in a switch like the macro-based version I showed you. Those will probably perform better than the alternatives, though only measurements can of course tell you in the end. Note that if there are differences in the frequency the cases occur, you need to at least sort the cases in that order (most occurring one at the top). "Some":http://www.eventhelix.com/realtimemantra/basics/optimizingcandcppcode.htm even suggest to split up the switch in multiple nested blocks. Not nice for readability, but perhaps better for speed (again: measure to be sure).
Edit:
Then again: realize that premature optimization is the root of many programming issues. Are you sure this is your time-critical bottleneck? How did you determine that? -
Never assume such things, especially if you are about to sacrifice maintainabiltiy of your code for it.
My first concern is to write code that I (and others) can read and understand later, and can modify if needed later on. Only when profiling shows that a section is time critical, I spend the effort to optimize the design of the code in that area.
-
[quote author="Andre" date="1321968332"]Never assume such things, especially if you are about to sacrifice maintainabiltiy of your code for it.
My first concern is to write code that I (and others) can read and understand later, and can modify if needed later on. Only when profiling shows that a section is time critical, I spend the effort to optimize the design of the code in that area. [/quote]
You right. Thank you.
I think i'll use rokemoon's solution.
And thank you everybody for respond and advices -
So,
just some side notes:
switch / case results in a jump (which is fast)
The solution of BilbonSacquet with the arrays result in 3 index based array accesses, which should also be fast.
The solution of rokemoon with the QMap results in a binary search, which should not be too slow, but I assume it slower then the array based access (as it is a search).
But to know, whether that really affects your app and makes it significant slower depends, how often you use it. If it is called 100 times in an msec, it could affect it, if it is called due to user input, forget optimization here. The user is slower ;-)
-
[quote author="Gerolf" date="1322037382"]So,
just some side notes:
switch / case results in a jump (which is fast)
The solution of BilbonSacquet with the arrays result in 3 index based array accesses, which should also be fast.
The solution of rokemoon with the QMap results in a binary search, which should not be too slow, but I assume it slower then the array based access (as it is a search).
But to know, whether that really affects your app and makes it significant slower depends, how often you use it. If it is called 100 times in an msec, it could affect it, if it is called due to user input, forget optimization here. The user is slower ;-)[/quote]
Thanks :)
And it's called maximum 10 times per sec.