Archived

This topic is now archived and is closed to further replies.

are if statements optimized out of a state machines compiled code

This topic is 5047 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Guest Anonymous Poster
Use a switch statement (don''t put in a default section) and any decent compiler will convert it into a jump table for you. That way it is a simple non-conditional operation to jump to the appropriate code for your state instead of a bunch of expensive conditionals.

Share this post


Link to post
Share on other sites
A number of methods have been developed for compile time branch prediction, avoiding the overhead of control flow changes. Googling for "branch prediction" offered a slew of informative and highly readable articles.

Another related goodie to look up is predication, which is becoming more prevalent on newer processor architectures. Finally, with newer languages like C# there exists a multitude of chances to optimize code flow branches with runtime profiling and re-just-in-time-compiling the code on the next execution, with the hindsight of profile data to optimize likely control flow.

Share this post


Link to post
Share on other sites
Some of it is also platform specific. Processors handle branches different they are one of the most difficult portions of code to pipeline. And for this reason they are also of course the hardest to optimize. (However this is such an old problem that the SPARC chips and Hyperthreading may have found a way to better solve the problem.) Also it would be cool to see how multiprocessor handle the same problem... but I don''t know I''ll leave the for later.

Share this post


Link to post
Share on other sites