wu :: forums
« wu :: forums - Optimizing switch statements »

Welcome, Guest. Please Login or Register.
Apr 17th, 2025, 9:58pm

RIDDLES SITE WRITE MATH! Home Home Help Help Search Search Members Members Login Login Register Register
   wu :: forums
   riddles
   cs
(Moderators: william wu, towr, Eigenray, Icarus, SMQ, Grimbal, ThudnBlunder)
   Optimizing switch statements
« Previous topic | Next topic »
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print
   Author  Topic: Optimizing switch statements  (Read 1424 times)
pharaoh
Newbie
*





   


Gender: male
Posts: 50
Optimizing switch statements  
« on: Aug 24th, 2010, 10:46am »
Quote Quote Modify Modify


Hi folks,
 
I am aware of various switch case opimization techniques, but as per my understanding most of the modern compilers do not care about how you write switch cases, they optimize them anyway.
 
Here is the issue:
 
void func( int num)
 
set = 1,2,3,4,6,7,8,10,11,15
 
{
     if (num is not from set )
  regular_action();
     else
      unusual_stuff();
}
 
The set would always have values mentioned above or something resembling with many of the elements closely spaced.  
 
E.g.  
 
set = 0,2,3,6,7,8,11,15,27 is another possible value.
 
The passed no is not from this set most of the times during my program run,  but when it is from the set I need to take some actions.
 
I have tried to simulate the above behavior with following functions just to figure out which way the switch statement should be written. Below functions do not do anything except the switch case - jump tables - comparisons.
 
I need to determine whether compare_1 is faster or compare_2 is faster. On my dual core machine,  compare_2 always looks faster but I am unable to figure out why doe this happen? Is the compiler so smart that it optimizes in such cases too?
 
 
#define MAX 100000000
void compare_1(void)
{
   unsigned long i;
   unsigned long j;
   printf("%s\n", __FUNCTION__);
   for(i=0;i<MAX;i++)
   {
      j = rand()%100;
      switch(j)
      {
    case 1:
    case 2:
    case 3:
    case 4:
    case 6:
    case 7:
    case 8:
    case 10:
    case 11:
    case 15:
       break   ;
    default:
       break   ;
      }
   }
}
 
void unreg(void)
{
   int i;
   int j;
   printf("%s\n", __FUNCTION__);
   for(i=0;i<MAX;i++)
   {
      j = rand()%100;
      switch(j)
      {
    default:
       break   ;
    case 1:
    case 2:
    case 3:
    case 4:
    case 6:
    case 7:
    case 8:
    case 10:
    case 11:
    case 15:
       break   ;
      }
   }
}
 
 
Pharaoh. Code:
IP Logged

Cogito ergo sum.

-Pharaoh
SMQ
wu::riddles Moderator
Uberpuzzler
*****






   


Gender: male
Posts: 2084
Re: Optimizing switch statements  
« Reply #1 on: Aug 24th, 2010, 11:15am »
Quote Quote Modify Modify

I think which one ends up being faster is going to depend far more on A) your compiler and B) the exact chip the program is running on than it will depend on which form you use in your C code.  Without looking at the assembly generated by the compiler, I would naively expect the first version to be (very slightly) faster since there should be one fewer jump needed for the common execution path than for the uncommon path.  However, with an optimizing compiler and a modern branch-predicting processor, the naive argument is likely made irrelevant.
 
If you really need to be that concerned about optimizing this one switch statement, perhaps you'd be better off working directly in assembly?  But if the actual work done in the common case is more than a couple operations, there's no real point in optimizing the switch statement anyway, as the very slight time savings will be swamped by the overall execution time...  with a decent compiler I would expect the difference between a "good" and a "bad" switch statement to be, at worst, two unnecessary comparisons and a single unnecessary pipeline flush--maybe 15 cycles in total.  You have to ask yourself if in the broader context of the function, are those 15 cycles really worth the effort?
 
--SMQ
IP Logged

--SMQ

Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print

« Previous topic | Next topic »

Powered by YaBB 1 Gold - SP 1.4!
Forum software copyright © 2000-2004 Yet another Bulletin Board