From: Frederick Williams on
"Alf P. Steinbach /Usenet" wrote:
>
> * 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.

Nor are they meant to be; they are merely schematic.

> In the first you have boolean expressions.

Oh, C is quite happy with

if (P)
...

were P is an integer!

--
I can't go on, I'll go on.
From: Alf P. Steinbach /Usenet on
* Frederick Williams, on 18.07.2010 18:02:
> "Alf P. Steinbach /Usenet" wrote:
>>
>> * 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.
>
> Nor are they meant to be; they are merely schematic.

So was my answer.


>> In the first you have boolean expressions.
>
> Oh, C is quite happy with
>
> if (P)
> ...
>
> were P is an integer!

So?

Do you think the first snippet does anything like the second?

It doesn't.


Cheers,

- Alf

--
blog at <url: http://alfps.wordpress.com>
From: Frederick Williams on
"Alf P. Steinbach /Usenet" wrote:
>
> * Frederick Williams, on 18.07.2010 18:02:
> > "Alf P. Steinbach /Usenet" wrote:
> >>
> >> * 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.
> >
> > Nor are they meant to be; they are merely schematic.
>
> So was my answer.
>
> >> In the first you have boolean expressions.
> >
> > Oh, C is quite happy with
> >
> > if (P)
> > ...
> >
> > were P is an integer!
>
> So?
>
> Do you think the first snippet does anything like the second?
>
> It doesn't.

I know, and my "Nor are they meant to be; they are merely schematic."
was supposed to indicate that they weren't meant to be and were merely
schematic.
--
I can't go on, I'll go on.
From: Daniel T. on
brangdon(a)cix.co.uk (Dave Harris) wrote:

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

I'm curious about the above assertion. Given that the question is
specific to the C language, and C allows fall through in switch
statements, is it really the case that the order of tests doesn't matter?
From: Willem on
Daniel T. wrote:
) I'm curious about the above assertion. Given that the question is
) specific to the C language, and C allows fall through in switch
) statements, is it really the case that the order of tests doesn't matter?

Yes, that is really the case.

The case labels are just jump points, and all of the testing
is done by that one switch statement, in whatever order.

It could even be a jump table or something similar, and it's
often a binary search.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
First  |  Prev  |  Next  |  Last
Pages: 1 2 3
Prev: Ariadne Designs Ltd
Next: ANN: Seed7 Release 2010-07-18