Thread: SWITCH and ARRAY which is efficient?

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    3

    SWITCH and ARRAY which is efficient?

    Which is the most efficient code in the below two, and why?

    In the below code X123 can have values X0, X1, X2 and X3 only.


    Code:
    switch(X123)
    {  case 0:
                  var1 = X0;
                  break;
      case 1:
                  var1 = X1;
                  break;
      case 2:
                  var1 = X2;
                  break;
      case 3:
                  var1 = X3;
                  break;
    
    }
    [\code]



    In the below code rewritten we use X[3] inatead of X0, X1...

    Code:
    If (X123 <= 3)
    {          var1= X[X123];
    }

  2. #2
    Registered User
    Join Date
    Jan 2011
    Posts
    55
    The switch statment in this case is more efficient. Sometimes it's a decision of how elegent you want your code to look, or how elegent you want to solve a problem.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    3
    Here my concern is the run time.

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    55
    With any modern day computer...Unless you have like thousands of lines of comparison statements...it won't matter too much. What is the actual program you are implementing the switch statement in?

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I would use whatever method you think is the easiest to understand. You should not worry about execution time until you have determined that you have a bottleneck. Premature optimization usually will just add complexity to your program with no real performance gain.

    Jim

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    55
    Quote Originally Posted by jimblumberg View Post
    I would use whatever method you think is the easiest to understand. You should not worry about execution time until you have determined that you have a bottleneck. Premature optimization usually will just add complexity to your program with no real performance gain.

    Jim
    Ha-ha, Just listen to Jim...

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    3
    Quote Originally Posted by DuckCowMooQuack View Post
    Ha-ha, Just listen to Jim...
    Thanx Guys

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    When it comes to run-time performance, lookup tables FTW! Shorter and likely faster. Having said that, if it matters at all - profile!
    Unless of course it is very important to stress that a value of 2 must be translated exactly into a value of X2 and not X1 say. I.e. in a long lookup table you cant easily see which input value translates to a certain output value. Then a switch table can be used for clarity.
    Often though, this is not important. Lookup tables are preferred in cases where say e.g. you want to select a random entry from a list of strings. The ordering of the table entries does not matter and using a switch statement would be longer, likely slower, and convey superflous information.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    I wouldn't be surprised if the compiler turned the switch statement into the array lookup on its own.

    You could profile it, but that doesn't tell you much, because modern CPUs are so complicated that the switch might be faster on one specific processor and slower on some other processor. Chances are they are not very much different anyway.

    Write the code to be clear. If and only if you then discover that it's not fast enough, use a tool -- do not guess -- to figure out where the bottleneck is and try to optimize it. Do experiments, don't just guess.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using a character array in a switch question.
    By bajanElf in forum C Programming
    Replies: 10
    Last Post: 11-08-2008, 08:06 AM
  2. What is more efficient?
    By swgh in forum C++ Programming
    Replies: 11
    Last Post: 10-18-2007, 04:03 AM
  3. switch from 2d array to 1d array
    By sass208 in forum C Programming
    Replies: 12
    Last Post: 12-11-2006, 09:34 AM
  4. efficient array
    By keithmolo in forum C++ Programming
    Replies: 3
    Last Post: 08-06-2003, 03:21 PM
  5. Replies: 3
    Last Post: 02-19-2003, 08:34 PM

Tags for this Thread