From: Frederick Williams on
In C is one of these faster than the other:

if (P)
A;
else if (Q)
B;
else if (R)
C;
...
else
D;


switch (X)
{
case P:
A;
break;
case Q:
B;
break;
case R:
C;
break;
...
default:
D;
}

I realize it will depend on the compiler, and possibly the hardware, but
"in general"?

--
I can't go on, I'll go on.
From: Richard Heathfield on
Frederick Williams wrote:
> In C is one of these faster than the other:

Several answers here.

Firstly, the C language specification (the relevant ISO Standard) does
not impose any performance requirements on implementations, either
absolute ("must be < 60 clocks") or relative ("this construct must be
faster than that construct").

Secondly, "premature optimisation is the root of all evil". Whilst
always keeping the code easy to read and maintain, make it work. Then
*measure*. If and only if the measurements reveal that the code is too
slow (check your requirements spec for performance requirements), look
for ways to speed it up. That doesn't mean that you shouldn't choose
good algorithms, and it doesn't mean that you shouldn't deliberately
make code slow, but speed is not the first criterion. If the code
doesn't work, it doesn't matter how fast it runs.

Thirdly, a switch /can/ be faster than an if, because compilers are at
liberty to build a jump table for switches. (Having said that, there's
no particular prohibition on them doing the same for if, but it's more
common in a switch.)

Fourthly, if is more flexible than switch, because you can use
expressions other than integer constants in an if.

Fifthly, if you haven't yet discovered memcpy and strcpy, the
performance difference between if and switch is really rather an
academic subject - like a learner driver (3 lessons so far, not too
happy about turning a corner across traffic) asking for the best
downforce settings to get you round a hairpin quickly.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Sig line vacant - apply within
From: Alf P. Steinbach /Usenet on
* Frederick Williams, on 18.07.2010 15:24:
> In C is one of these faster than the other:
>
> if (P)
> A;
> else if (Q)
> B;
> else if (R)
> C;
> ...
> else
> D;
>
>
> switch (X)
> {
> case P:
> A;
> break;
> case Q:
> B;
> break;
> case R:
> C;
> break;
> ...
> default:
> D;
> }
>
> I realize it will depend on the compiler, and possibly the hardware, but
> "in general"?

They're not equivalent code snippets. In the first you have boolean expressions.
In the second you have different values of X. So which is fastest of quicksort
and flood fill? Difficult to say.


Cheers & hth.,

- Alf

--
blog at <url: http://alfps.wordpress.com>
From: Richard Heathfield on
Richard Heathfield wrote:
> Frederick Williams wrote:
>> In C is one of these faster than the other:
>
<snip>

>
> Fifthly, if you haven't yet discovered memcpy and strcpy, the
> performance difference between if and switch is really rather an
> academic subject - like a learner driver (3 lessons so far, not too
> happy about turning a corner across traffic) asking for the best
> downforce settings to get you round a hairpin quickly.

Scratch this one. Sorry, Frederick. I was mixing you up with someone
else. The other four reasons all apply, though.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Sig line vacant - apply within
From: Dave Harris on
frederick.williams2(a)tesco.net (Frederick Williams) wrote (abridged):
> In C is one of these faster than the other:

Generally a "switch" statement will be at least as fast as a chain of "if"
statements.

The reason is, most modern compilers are smart enough to use "if"
statements to implement the "switch" if it would be better. It's harder
and rarer for them to replace a chain of "if" statements with a jump
table.

Going slightly deeper: "switch" tells the compiler up-front that the
expression need only be evaluated once, and that the order of tests
doesn't matter. That gives it a lot of freedom to organise the code most
efficiently. It might arrange the tests to form a binary search, for
example. If it uses a table, it might pad out the values so the look-up
is a single indirection rather than a looping search. If it has profiling
information about run-time performance, it can arrange for the most
common cases to be tested first.

The main exception, when "if" statements are faster, are when you know
more about circumstances than the compiler. If the compiler doesn't offer
profile-guided optimisation, but you happen to know that X==Q will be
true 99% of the time, you can get faster code by putting that test first
manually.

(Which isn't to say "switch" should be preferred over "if" in real code,
because performance isn't the only criteria of code quality. If
performance matters to the point that you'd consider writing less clear
code, you need to measure it. Then there is no "in general".)

-- Dave Harris, Nottingham, UK.
 |  Next  |  Last
Pages: 1 2 3
Prev: Ariadne Designs Ltd
Next: ANN: Seed7 Release 2010-07-18