Age old question.. Use if else or switch

I’m asking the same question but I would like to know which is better to use for the .NET Micro Framework.

Assume for 10 or more type string questions or type numerical states to be solved.

Speed is not a issue.

Which uses less code and/or objects created.

Not sure I am asking the question properly because I do not know how the interpreter works.

Switch is made into ifs by compiler IIRC so its really just for readability.

@ Skewworks -

Thanks for the reply.

Not entirely true… That may happen when the compiler takes the IL code down to machine code. I’m not sure how to verify that… But, we can examine the IL code and that reveals some big differences. I compiled these three blocks of code separately then inspected them with ILDASM.


        public static void Main()
        {
            var x = 10;
            var y = 0;

            // IF...ELSE IF
            if (x == 1) y = 1;
            else if (x == 2) y = 2;
            else if (x == 3) y = 3;

            // IF
            if (x == 1) y = 1;
            if (x == 2) y = 2;
            if (x == 3) y = 3;

            // SWITCH
            switch (x)
            {
                case 1:
                    y = 1;
                    break;
                case 2:
                    y = 2;
                    break;
                case 3:
                    y = 3;
                    break;
            }

            Console.WriteLine(y);
        }

In the attached image, you can see the three different IL versions side-by-side. For a desktop app, the differences may be insignificant. However, for our tiny devices where decreasing the file size can sometimes be very important when people have maxed out the flash space it is very interesting that the SWITCH version is 12 lines shorter than the IF…ELSE IF version. However, it turned out that the actual file sizes were all exactly the same which makes me wonder if there is a minimum assembly size that is being enforced there. My assumption, though, is that if you had a much larger app with several SWITCH statements in it then the overall assembly size would be smaller than if just using IFs blocks and even smaller than if using IF…ELSE IF blocks. We would still expect faster execution, though, between IF…ELSE IF vs. just IF due to short-circuiting.

@ willgeorge - I encourage you to check out ILDASM and do some of your own tests. I’d be especially interested in seeing the result of a test with lots of SWITCH statements vs lots of IF…ELSE IF blocks.

2 Likes

Remeber that in NETMF there is never any machine code. The IL is interpreted directly, and thus, the number of instructions likely has a large impact on performance.

Switch has the potential to be more performant. On the desktop framework, at least, switch statements are allegedly implemented as a hash lookup instead of a bunch of ldc and ceq instructions.

Switches just seem so much more elegant and readable if there are more than 3 options.

I spend a lot of time replacing awkward “else if” trees written by Neanderthals, with sanely formatted switch statements.

1 Like

@ mtylerjr -

Neanderthals

You mean “knuckle draggers”?