By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,820 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,820 IT Pros & Developers. It's quick & easy.

case execution time

P: n/a
Hi,

I have a large 'case' block (around 60 values to test). I was wondering how
much impact the comparison time would have on performance. Could this
penalty be ignored?

Cristian
Jul 19 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Cristian Tots wrote:
Hi,

I have a large 'case' block (around 60 values to test). I was wondering how
much impact the comparison time would have on performance. Could this
penalty be ignored?

Impossible to say, as it's a quality-of-implementation issue -- or
perhaps not even that.

As always with optimization questions, if things are not running
quickly enough, profile -- and once you're sure that something is a
bottleneck, look into other options.

HTH,
--ag

--
Artie Gold -- Austin, Texas
Oh, for the good old days of regular old SPAM.

Jul 19 '05 #2

P: n/a
The overhead of a case statement depends upon the compiler and
the relative closeness of the different case values. If the values
are very close, the compiler might generate a case variable indexed
jump table.

Some compilers perform a binary search when the number of entries is
this large.

Checkout the following article for more details.

http://www.eventhelix.com/RealtimeMa...AndCPPCode.htm

For the inner workings of a case statement are covered in the
following article:

http://www.eventhelix.com/RealtimeMa...anslation3.htm

Sandeep
--
http://www.EventHelix.com/EventStudio
EventStudio 2.0 - Generate Sequence Diagrams and Use Case Diagrams in PDF
Jul 19 '05 #3

P: n/a
Cristian Tots wrote:
Hi,

I have a large 'case' block (around 60 values to test). I was
wondering how much impact the comparison time would have on
performance. Could this penalty be ignored?


That depends on the compiler and maybe on the values you test, but
often, there is no comparison and switch/case is _very_ fast.

Jul 19 '05 #4

P: n/a
Thanks,

I guess it's better to use an array of function pointers instead. I haven't
implemented it yet but it would get rid of the switch.

Cristian

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:bo*************@news.t-online.com...
Cristian Tots wrote:
Hi,

I have a large 'case' block (around 60 values to test). I was
wondering how much impact the comparison time would have on
performance. Could this penalty be ignored?


That depends on the compiler and maybe on the values you test, but
often, there is no comparison and switch/case is _very_ fast.

Jul 19 '05 #5

P: n/a
On Tue, 11 Nov 2003 16:29:00 +0200, "Cristian Tota"
<cr***********@vion-software.ro> wrote:
Thanks,

I guess it's better to use an array of function pointers instead. I haven't
implemented it yet but it would get rid of the switch.


However, the performance benefit from the code for each case being
inlined might outweight the benefit you get in just doing an array
dereference. Calling through a function pointer disables inlining
(usually).

Tom
Jul 19 '05 #6

P: n/a
Cristian Tota wrote:
Thanks,

I guess it's better to use an array of function pointers instead. I
haven't implemented it yet but it would get rid of the switch.


As I wrote, depending on the compiler and the values, switch/case may be
very fast, and its complexity can be O(1), i.e. not dependant on the
number of cases you have. Many people think of switch/case as just
another way to write an if/else if cascade, but in fact, compilers may
(and often do) choose to implement it as a jump table with the
to-be-tested value as index into that table, which makes it faster than
any other alternative.

Jul 19 '05 #7

P: n/a
tom_usenet wrote:
On Tue, 11 Nov 2003 16:29:00 +0200, "Cristian Tota"
<cr***********@vion-software.ro> wrote:

Thanks,

I guess it's better to use an array of function pointers instead. I haven't
implemented it yet but it would get rid of the switch.

However, the performance benefit from the code for each case being
inlined might outweight the benefit you get in just doing an array
dereference. Calling through a function pointer disables inlining
(usually).


Just to emphasis the point.

Once upon a time (back in 1982), I profiled some C code
compiled on an 16 bit computer and found that 40% of the
time was spent converting 16 bit ints to 32 bit ints doing
pointer arithmetic. Using that particular compiler
microlevel optimizations were a complete waste of time.

Microlevel optimization ought to be the last thing you do,
after you have profiled your code and really discovered
where the bottlenecks are. As others have said these
optimizations may gain you a few percentage points in
performance. Smarter algorithms and data structures, on the
other hand can gain you orders of magnitude better performance.

Jul 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.