My buddy says that he tries to program with as little if/else statements as possible, for efficiency's sake. When I asked why he said that if/else's take a somewhat significant part of the program's ressources, so he tries to stay away from them.
Is he right? Are there better ways to execute if/else style code without actually using that structure? Are switch/case structures any better?
开发者_如何学PythonEDIT: He wasn't referring to a specific language but more as a general practice. As well as UNIX/Linux and Windows platforms.
Typically, a compiler will represent an if/then/else construct as a test followed by a jump to another location in the code. This is a very standard and well-optimized operation for any processor. Unless you are programming in something other than the standard compiled or interpreted languages, your buddy's conclusion is seriously flawed.
Further, optimizing code at this level without profiling it first (to find where the slow bits are) is well, ill-advised. (More like a complete waste of the programmer's time.)
switch/case statements have the same speed as if statements with all modern optimizing compilers.
Your buddy is writing spaghetti code.
If statements can indeed be slow relative to switch/case. Here's a good explanation from SO: If vs. Switch Speed
Here's an interesting article going into some of the minutia from a C# standpoint: http://igoro.com/archive/fast-and-slow-if-statements-branch-prediction-in-modern-processors/
That said, depending on the type of programs you write, it is likely that there are other factors to consider, such as algorithm design or the amount of I/O or network traffic required, that will have a much greater impact on performance. Unless those conditional statements are getting called bazillions of times, you will probably have a bigger payoff writing clean, readable code, and focusing on issues with more measurable performance impact.
Good luck!
As many people pointed out, most of the modern (C/C++) compilers handle this optimally. You may be interested in this link http://www.eventhelix.com/realtimemantra/basics/optimizingcandcppcode.htm#Break Big Switch Statements into Nested Switches
Adds a few more points on how to use switch statements.
Modern apps can contain thousands of lines of code. That code is full of calls to database routines, data structure library functions, file I/O, socket I/O, Windowing functions, etc. etc.
So while running that code, if you take a bunch of snapshots of its state, it is almost always in some function the application programmer didn't write. It's pretty typical for the program counter to be in the programmer's own code for 10%, 1%, or even less of the time.
So any optimization in the application code to save a cycle here, a cycle there, will have relevance only in the fraction of time that the program counter is in that code - between little, and very little, in that type of code.
So while a switch statement might save a few cycles if it has enough branches, it's useful to have a sense of perspective.
A sloppy programmer may write code that heavily relies on several similar conditions when a lookup table would have been the better choice. Moral of the story: avoid unnecessary branching (although that should be obvious).
Slowdowns due to branching are often caused by instruction pipeline stalls, although the severity of such slowdowns is architecture-dependent.
精彩评论