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
websssCEOAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

É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
websssCEOAuthor 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
É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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

websssCEOAuthor 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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.