From -3466796998256188841
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,5be30c38f618a07b
X-Google-Attributes: gidf78e5,public
X-Google-ArrivalTime: 2002-05-05 14:27:01 PST
Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!kibo.news.demon.net!news.demon.co.uk!demon!mail2news.demon.co.uk!not-for-mail
From: Gennaro Prota <gennaro_prota@yahoo.com>
Newsgroups: comp.std.c++
Subject: Re: Why switch w/ only integral types?
Date: Sun,  5 May 2002 21:26:15 GMT
Organization: [Infostrada]
Approved: Fergus Henderson <fjh@cs.mu.oz.au>, moderator of comp.std.c++
Message-ID: <l978du02tu8ettaobk3cob8jcuns7d9p9u@4ax.com>
References: <6ee7d287.0203202005.63e0b7b3@posting.google.com> <qfpm8.12422$cy2.680922353@newssvr21.news.prodigy.com> <3CCE6310.672E55DF@webmaster.com> <aatv35$dm69h$1@ID-97366.news.dfncis.de>
X-Trace: mail2news.demon.co.uk 1020633979 mail2news:23495 mail2news mail2news.demon.co.uk
X-Complaints-To: abuse@demon.net
X-Mail2News-Path: news.demon.net!mulga.cs.mu.oz.au
X-Authentication-Warning: mulga.cs.mu.OZ.AU: fjh set sender to devnull@stump.algebra.com using -f
X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov)
X-Newsreader: Forte Agent 1.9/32.560
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
NNTP-Posting-Date: Sat, 04 May 2002 19:56:20 MET DST
Lines: 70
Xref: archiver1.google.com comp.std.c++:11051

On Fri,  3 May 2002 17:34:09 GMT, "Pavel Kuznetsov"
<pavel@despammed.com> wrote:

>David Schwartz (davids@webmaster.com) wrote:
>
>DS> and on many platforms is actually slower than 
>DS> corresponding if/else code at low levels of optimization.
>
>At least on one platform code generated from switch/case
>is a lot faster than that from if/else if. And of course 
>speed is not right factor for choosing between if/else 
>and case. Readability and ease of maintainance is.

NOTE: this is not actually a reply to Pavel, but rather a general
remark.

Usually I do not participate in "this is faster... this is less
efficient" threads: they tend to be a quite sterile fight of opinions,
supported by reasonings that base on vagueness their apparent
plausibility. Indeed there is little you can say at this level which
is not an opinion.

The definite answer to the switch/if-else dilemma, as well as to other
similar questions, is *you can't know in general*.

Not even if you go to the assembly output: yes,  you see the output
for a specific source code snippet, but the output could actually
depend on how many case labels you have, whether they all have a
break, whether they have consecutive values or, let's say, whether the
compiler manages to derive a formula that expresses those values as a
function of n, where n is an integer, etc...etc....

Certainly, an implementation is free to do as much analysis as it
wants and translate the same way a switch construct and its
semantically equivalent if-else (I'm not speaking of variable
scope/lifetime and other stuff, obviously).

And "the same way" might even mean equally bad, or equally well.

Now, if your profiler indicates that you must  optimize let's say an
if-else snippet and you *verify* that, in that place, a switch/case is
actually faster, than do the change (and add some comments). Otherwise
don't do harm to you, to anyone will maintain the code and definitely
to your employer choosing the less clear/readable/expressive form.

If you are not convinced about that (humans tend to prefer simple
answers to correct one, i.e. "yes" or "no" to "it depends") please
take a look at

http://developer.intel.com/technology/itj/q41999/articles/art_1.htm

expecially the parts about predication and if-conversion. And since
you are there, take a look at the part about loop unrolling too.

>From our C++ sources we are on a too high level of abstraction to
decide whether a construct is less efficient than another, less than
ever to say if it is *always* more efficient.

Of course I'll be glad to be corrected on any of my statements :)


Genny.


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]



