473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

case execution time

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
7 3384
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Johannes Lebek | last post by:
Hi there, lately, I experienced a strange thing on my DB2 V8.1 on Windows: Some queries took a very long time. A snapshot discovered the following: Number of executions = 47...
38
by: vashwath | last post by:
Might be off topic but I don't know where to post this question.Hope some body clears my doubt. The coding standard of the project which I am working on say's not to use malloc.When I asked my...
3
by: iam980 | last post by:
Hello All. We have tested following SQL script from query analyzer: -- Script begin DECLARE @I int; SET @I = 1; WHILE @I < 10000000 BEGIN SET @I = @I + 1; END -- Script end
2
by: Ina Schmitz | last post by:
Hi NG, does IBM Universal Database 8.2 make any difference between actual and estimated execution plans like in SQL Server ("set showplan_all on" for estimated execution plan and "set statistics...
5
by: alacrite | last post by:
If I have code like this: int main() { a=1; b=2; x=0; switch(a) {
22
by: John | last post by:
Hi Folks, I'm experimenting a little with creating a custom CEdit control so that I can decide on what the user is allowed to type into the control. I started off only allowing floating point...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
1
by: Don Li | last post by:
Hi, Env: MS SQL Server 2000 DB Info (sorry no DDL nor sample data): tblA has 147249 rows -- clustered index on pk (one key of datatype(int)) and has two clumns, both are being used in joins;...
6
by: Mikhail Kovalev | last post by:
I'm using set_time_limit() to set maximum execution time. Is there a way to check how much time is left at any time during the execution itself?
13
by: Satya | last post by:
Hi everyone, This is the first time iam posting excuse me if iam making any mistake. My question is iam using a switch case statement in which i have around 100 case statements to compare. so...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.