What is optimal algorithm for switch case
-
wrote on 22 Nov 2011, 13:22 last edited by
Program receive messages from devices and do some action with result of switch()
Yes it is maybe not so critical but i assume speed is more important. -
wrote on 22 Nov 2011, 13:25 last edited by
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.
-
wrote on 22 Nov 2011, 13:34 last edited by
[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 -
wrote on 23 Nov 2011, 08:36 last edited by
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 ;-)
-
wrote on 23 Nov 2011, 10:59 last edited by
[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.
-
wrote on 23 Nov 2011, 11:44 last edited by
[quote author="qxoz" date="1322046932"]
I've already written an algorithm based on yours solution. ;)[/quote]
I'm glad to hear it :-) -
wrote on 23 Nov 2011, 13:47 last edited by
[quote author="qxoz" date="1322045941"]And it's called maximum 10 times per sec.[/quote]
Don't care about speed optimization, care about maintainability then :-)
-
wrote on 23 Nov 2011, 15:47 last edited by
[quote author="Gerolf" date="1322056043"][quote author="qxoz" date="1322045941"]And it's called maximum 10 times per sec.[/quote]
Don't care about speed optimization, care about maintainability then :-)
[/quote]I will. Thank you.
23/23