Post Reply 
newRPL: [UPDATED April 27-2017] Firmware for testing available for download
10-04-2016, 01:31 PM
Post: #402
RE: newRPL: [UPDATED September-17-16] Firmware for testing available for download
(10-04-2016 12:16 PM)Bruno Wrote:  Why did you use a slow O(n) switch/case structure in the library handlers instead of a fast O(1) structure like an array of function pointers ?

Good point. Lately, all compilers automatically generate a jump table when compiling switch statements with consecutive numbers, so from a speed point of view it's almost equivalent, except you don't need to explicitly code the array or the jump.
Also, using separate functions causes additional overhead in passing arguments, setting up variables, etc. that could be shared between all cases. See for example how operators are handled on lib-twelve, where reading the arguments is common code at the beginning, the switch is done after we know more about the arguments. If we were to put this into separate functions, it would require overhead passing arguments, which we don't really need, or duplicate the argument reading code in each and every function, bloating the code, or having the argument reading on a separate function, causing even more overhead.
From a coding perspective, a switch() allows to "fall through" the next case, which is useful in many parts of the code (for example, subtraction is sometimes handled as negating the second argument and falling through the addition case).

So I think our use case is a very good fit for a switch() statement, the only drawback is that the code is less readable, but I found out that using a set of brackets { } in each case statement allows the code editor to collapse the whole statement, so it's not that bad in the end (if you use QT Creator or another good IDE, that is).
Find all posts by this user
Quote this message in a reply
Post Reply 

Messages In This Thread
RE: newRPL: [UPDATED September-17-16] Firmware for testing available for download - Claudio L. - 10-04-2016 01:31 PM

User(s) browsing this thread: 1 Guest(s)