should I convert 42 ELSE IFs to a CASE?

I've inherited a bit of code
I want to make some performance improvements

there are many areas to focus on
but one of the small things i've noticed is there are 42 else if statements

Someone suggested the compiler deals with stuff like this
however, just curious to know if there any performance gains to be had

the app in question reads /writes 100's of message queue items per second
websssAsked:
Who is Participating?
 
Éric MoreauSenior .Net ConsultantCommented:
because it is the same variable/property compared again and again, you will get a bit of improvement:

switch (item.Name.LocalName)
{
   case "GPS_Accuracy":
      gpsaccuracy = item.Value;
      break;
   case "speed":
      speed = item.Value;
      break;
...
}

Open in new window

0
 
Éric MoreauSenior .Net ConsultantCommented:
comparing the same variable to 42 values? yes you can get little performance improvement. Can we get an idea of the code?
0
 
websssAuthor Commented:
here is a snap shot of some
Its string matching:

    else if (item.Name.LocalName == "GPS_Accuracy")
                            {
                                gpsaccuracy = item.Value;
                            }
                            else if (item.Name.LocalName == "speed")
                            {
                                speed = item.Value;
                            }
                            else if (item.Name.LocalName == "azimuth")
                            {
                                azimuth = item.Value;
                            }
                            else if (item.Name.LocalName == "latitude")
                            {
                                latitude = item.Value;
                            }
                            else if (item.Name.LocalName == "longitude")
                            {
                                longitude = item.Value;
                            }
                            else if (item.Name.LocalName == "altitude")
                            {
                                altitude = item.Value;
                            }
                            else if (item.Name.LocalName == "gps_utc_time")
                            {
                                gpsUtcTime = item.Value;
                            }
                            else if (item.Name.LocalName == "MCC")
                            {
                                mcc = item.Value;
                            }
                            else if (item.Name.LocalName == "MNC")
                            {
                                mnc = item.Value;
                            }
                            else if (item.Name.LocalName == "LAC")
                            {
                                lac = item.Value;
                            }
                            else if (item.Name.LocalName == "cell_id")
                            {
                                cellid = item.Value;
                            }
                            else if (item.Name.LocalName == "odometer")
                            {
                                odometer = item.Value;
                            }
                            else if (item.Name.LocalName == "ign_time_count")
                            {
                                ignTimeCount = item.Value;
                            }
                            else if (item.Name.LocalName == "analog_input_1")
                            {
                                analogInput1 = item.Value;
                            }
                            else if (item.Name.LocalName == "backup_batt")
                            {
                                backupBatt = item.Value;
                            }
                            else if (item.Name.LocalName == "send_time")
                            {
                                sendTime = item.Value;
                            }
                            else if (item.Name.LocalName == "analog_input_vcc")
                            {
                                analogInputVcc = Convert.ToInt32(item.Value);
                            }
                            else if (item.Name.LocalName == "analog_input_id")
                            {
                                analogInputId = Convert.ToInt32(item.Value);
                            }
                            else if (item.Name.LocalName == "voltage_range")
                            {
                                voltageRange = Convert.ToInt32(item.Value);
                            }
                            else if (item.Name.LocalName == "ign_off_duration")
                            {
                                ignOffDurationVal = Convert.ToInt32(item.Value);
                            }
                            else if (item.Name.LocalName == "ign_on_duration")
                            {
                                ignOnDurationVal = Convert.ToInt32(item.Value);
                            }
                            else if (item.Name.LocalName == "motion_state")
                            {
                                motionState = Convert.ToInt32(item.Value);
                            }
                            else if (item.Name.LocalName == "idling_duration")
                            {
                                idlingDurationVal = Convert.ToInt32(item.Value);
                            }
                            else if (item.Name.LocalName == "speed_level") // in int
                            {
                                speedLevel = Convert.ToInt32(item.Value);
                            }
                            else if (item.Name.LocalName == "hb_type") // in bool
                            {
                                hbType = Convert.ToBoolean(Convert.ToInt16(item.Value));
                            }
                            else if (item.Name.LocalName == "speed_status") // in bool
                            {
                                speedStatus = Convert.ToBoolean(Convert.ToInt16(item.Value));
                            }
                            else if (item.Name.LocalName == "batt_vcc") // in V(volt)
                            {
                                battVcc = item.Value;
                            }
                            else if (item.Name.LocalName == "digital_input_id")
                            {
                                digitalInputId = Convert.ToInt32(item.Value);
                            }
                            else if (item.Name.LocalName == "digital_input_stat")
                            {
                                digitalInputStat = Convert.ToInt32(item.Value);
                            }
                            else if (item.Name.LocalName == "count")
                            {
                                count = item.Value;
                            }
                            else if (item.Name.LocalName == "tailchar")
                            {
                                tailchar = item.Value;
                            }
                            else if (item.Name.LocalName == "IsAlert")
                            {
                                isAlert = item.Value;
                            }

Open in new window

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
websssAuthor Commented:
great, i'm all in for performance improvements!
0
 
Naitik GamitSoftware DeveloperCommented:
The compiler is better in optimizing a switch-statement than an if-statement.For just a few items, the difference is small. If you have many items you should definitely use a switch.

look at this Speed Test: Switch vs If-Else-If

http://www.blackwasp.co.uk/SpeedTestIfElseSwitch.aspx
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
re performance - I would expect changing strings to enumerated values would bring rather more than just changing to a switch....case construct.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.