![]() |
switch vs. if... else if. More efficient?
Greetings!
I'm programming the code for the CMUCam and serial interface right now and have just begun the portion where I determine the kind of data I am getting from the serial port buffer. I can imagine that this is an intensive process on the controller... so: would it be more efficient to use a switch statement or an else... else if chain to determine what the next character in the buffer is? I've read that a switch statement (out of many possibilities) would be more efficient because of compiler's ability to optimize the order of comparisons. But does this apply in the case of C18 2.4? Thank you! |
Re: switch vs. if... else if. More efficient?
The switch statement essentially constructs the same code as a stack of if/then/else statements.
The switch is useful because it is a way of documenting code and representing it as a 1 of n selector. This is a plenty fast processor for what you are probably going to try and do so I wouldn't worry about it. Worrying about these types of things for your i/o routines generally isn't necessary as it was say 30 years ago. |
Re: switch vs. if... else if. More efficient?
Always optimize for readability. The exceptions to this rule are _very_ few and far between.
|
Re: switch vs. if... else if. More efficient?
Agreed. Switch is the way to go IMO. But then again it depends on exactly what you're doing.
|
Re: switch vs. if... else if. More efficient?
documenting your code is especially important if your the sole student programmer on your team and your about to graduate :)
if thens might give you more control in some situations, but with state comparisons and what not, switches are pretty and convenient |
Re: switch vs. if... else if. More efficient?
Quote:
I had to learn how to program the robot by reading a printout of the code in the back of a van on the way to regionals. This nightmare situation was only made worse by their need to shave a clock cycle here and there. |
Re: switch vs. if... else if. More efficient?
Quote:
In the following test you can see using an "if..else if...else if" has a worse case path of 28 instruction and takes a literal approach of comparison. An example of a single test is shown below: Quote:
Code:
240: switch (data_type)Still other compilers will use a program jump table strategy to get the switch/case down to 4-8 cycles (compiler aligns a jump table on a 256 program word boundary and uses the switch variable to goto the appropriate entry in the table which then does a goto to the specific code entry. However, both "if..else if.." and switch are good and valid methods without significant difference except for possible readability. The only place it would really matter would be if the code is executed at interrupt level -- then you might be more concerned with code efficiency vs readability. Code:
221: void test_ifswitch( unsigned char data_type ) |
| All times are GMT -5. The time now is 23:12. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi