2

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?

EDIT: He wasn't referring to a specific language but more as a general practice. As well as UNIX/Linux and Windows platforms.

user83643
  • 2,901
  • 2
  • 17
  • 13
  • 3
    You will need to add information about the actual programming language and platform in question, but in all languages I know, your buddy's claim is utter nonsense. – Pekka Jun 03 '11 at 17:42
  • 1
    What does he do/use instead? Can you give an example? – Gumbo Jun 03 '11 at 17:44
  • I think this is largely a language dependent question, but perhaps fewer if/else statements can be tied to better semantic program design (as opposed to just a syntact improvement). –  Jun 03 '11 at 17:44
  • 2
    I'm guessing this is about mispredicted branches causing pipeline bubbles on superscalar processors, but unless you're doing really low level stuff this won't be worth mangling your code over. – hammar Jun 03 '11 at 17:46
  • This sounds more like a question for http://programmers.stackexchange.com – Code Maverick Jun 03 '11 at 17:47
  • 1
    I just decided to add a new thing to the list of things that will never happen and thus can be used in prose to say "never" in a more obscure way: No programmer (inappropriately) worries about the performance impact of irrelevant details anymore. –  Jun 03 '11 at 18:20
  • http://stackoverflow.com/questions/395618/if-else-vs-switch – Xodarap Jun 03 '11 at 18:30

6 Answers6

12

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.)

Rob Raisch
  • 17,040
  • 4
  • 48
  • 58
6

switch/case statements have the same speed as if statements with all modern optimizing compilers.

Your buddy is writing spaghetti code.

τεκ
  • 2,994
  • 1
  • 16
  • 14
  • I just googled spaghetti code. I had a laugh thanks @τεκ :) – user83643 Jun 03 '11 at 17:49
  • 3
    I would add that you could make code more efficient by putting conditions that are most likely to produce a false result before the others in the sequence. Since they are evaluated in the order they are in the code, this could improve speed. – Justin Jun 03 '11 at 17:51
  • My hunch is that this would ultimately be highly dependent on the compiler. This article seems to contradict the idea that they are equal in C#, but other languages/compilers may produce different results: http://www.blackwasp.co.uk/SpeedTestIfElseSwitch.aspx – Michael Ames Jun 03 '11 at 18:00
2

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!

Community
  • 1
  • 1
Michael Ames
  • 2,607
  • 1
  • 16
  • 22
2

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.

Tatvamasi
  • 2,537
  • 19
  • 14
2

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.

Mike Dunlavey
  • 40,059
  • 14
  • 91
  • 135
1

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.

Coleman S
  • 488
  • 1
  • 4
  • 15