Somewhere in a tutorial i read that if statement has performance
overheads as code within the if statement cannot take benefit of
pipeling of microprocessor and also that the compiler cannot agressively
optimize that code.
my functions as much as possible and also try to have minimum code
withing if block.
However, i am bit skeptic about this.
I need some guidance. Performance is always the key issue for me when it
comes to writing programs.
Please guide.
I wouldn't worry about it. If I wanted to avoid if statements in my code
I'd be in serious trouble. Only worry about optimizing what actually
needs optimizing.
To give you an example, I am working on a 2D CAD application for
electronics design. At the lowest level, I have a class called Scalar
that allows me to perform arithmetic with any 2 values, regardless of
unit type (inch, millimeter, etc.) with one another.
This being among the most essential and lowest-level class of them all,
it has every bit imaginable optimized out of it that I can think of.
Because even a single instruction saved in this class can translate into
hundred thousands or more instructions during a complex operation later
on. Here, speed matters more to me than code clarity.
Maybe I should sell it to NASA so that they can stop crashing things into
planets because they can't get their units straight. =)
Now, my higher level functions though, such as the code that can take 2
object outlines composed of line and curve segments and calculate the
distance or intersection between the two outlines, is somewhat optimized
but I don't overly worry about squeezing every last bit out of it. In a
worst case scenario this code might maybe be called a hundred or so times
in one shot. Clearly written code at the expense of speed is more
important here as it involves some complex operating.
So what I'm trying to get at is, unless the code is really speed critical
and you absolutely will benefit from every tiniest bit of optimization,
don't worry about it. Rather worry that your code is clearly written in a
way that you can still understand it when you come back to it 6 months
later. That'll benefit you far more. =)
--
Stephan
2003 Yamaha R6
å›ã®ã“ã¨æ€ã„出ã™æ—¥ãªã‚“ã¦ãªã„ã®ã¯
å›ã®ã“ã¨å¿˜ã‚ŒãŸã¨ããŒãªã„ã‹ã‚‰