Compare c # operators?: Vs if-else vs switch

Today, another animal factory flew into the review:

public static class AnimalsFactory
{
    public static Animal CreateAnimalByTernaryOperator(bool isCat)
    {
        return isCat ? (Animal)new Cat() : new Dog();
    }
}

Once again I was upset that C # forces you to cast the Cat object to Animal. But let the caste be better, because through the if-else statement the code is even longer:
public static class AnimalsFactory
{
    public static Animal CreateAnimalByIfElseOperator(bool isCat)
    {
        if (isCat)
            return new Cat();

        return new Dog();
    }
}

Let’s digress for a minute from the review and try to figure it out:

  • Will the IL code be different in these examples?
  • will one of the examples benefit in performance?


The answer to the first question is yes, the IL-code is different, I’ll show below what.

Let's move on to the issue of performance. Download the nuget package for BenchmarkDotNet benchmarks and write a test:

public class AnimalFactoryPerformanceTests
{
    [ParamsAllValues]
    public bool IsCat { get; set; }

    [Benchmark]
    public void CreateAnimalByTernaryOperator() =>
        AnimalsFactory.CreateAnimalByTernaryOperator(IsCat);

    [Benchmark]
    public void CreateAnimalByIfElseOperator() =>
        AnimalsFactory.CreateAnimalByIfElseOperator(IsCat);
}

Benchmark Results:

|                          | IsCat |     |
|------------------------------ |------ |---------:|
| CreateAnimalByTernaryOperator | False | 1.357 ns |
| CreateAnimalByTernaryOperator |  True | 1.655 ns |
|------------------------------ |------ |---------:|
|  CreateAnimalByIfElseOperator | False | 1.636 ns |
|  CreateAnimalByIfElseOperator |  True | 1.360 ns |

It is surprising that “for a dog” ternary works faster, and “for a cat” - an if-else statement.
We look at the IL-code of the method with the ternary operator:

CreateAnimalByTernaryOperator(bool isCat)
{
    IL_0000: ldarg.0      // isCat
    IL_0001: brtrue.s     IL_0009
    IL_0003: newobj       instance void AnimalPerformance.Dog::.ctor()
    IL_0008: ret
    IL_0009: newobj       instance void AnimalPerformance.Cat::.ctor()
    IL_000e: ret
}

When creating the Dog object, the IL_0000 - IL_0008 commands are executed sequentially , while when creating the Cat object , a conditional jump occurs ( IL_0001: brtrue.s IL_0009 ).

As you might guess, for the if-else statement, IL code is generated that does not require conditional jumps to create a Cat object . While the Dog object is created via a conditional branch:

CreateAnimalByIfElseOperator(bool isCat)
{
    IL_0000: ldarg.0      // isCat
    IL_0001: brfalse.s    IL_0009
    IL_0003: newobj       instance void AnimalPerformance.Cat::.ctor()
    IL_0008: ret
    IL_0009: newobj       instance void AnimalPerformance.Dog::.ctor()
    IL_000e: ret
}

Add the Parrot creation and a new method with the switch statement to the factory :

public static class AnimalFactory
{
    public static Animal CreateAnimalByTernaryOperator(AnimalType animalType)
    {
        return animalType == AnimalType.Cat
            ? new Cat()
            : animalType == AnimalType.Dog
                ? (Animal)new Dog()
                : new Parrot();
    }

    public static Animal CreateAnimalByIfElseOperator(AnimalType animalType)
    {
        if (animalType == AnimalType.Cat)
            return new Cat();

        if (animalType == AnimalType.Dog)
            return new Dog();

        return new Parrot();
    }

    public static Animal CreateAnimalBySwitchOperator(AnimalType animalType)
    {
        switch (animalType)
        {
            case AnimalType.Cat:
                return new Cat();
            case AnimalType.Dog:
                return new Dog();
            case AnimalType.Parrot:
                return new Parrot();
            default:
                throw new InvalidOperationException();
        }
    }
}

Which method will be faster?

Benchmark results
|                          | AnimalType |     |
|------------------------------ |----------- |---------:|
| CreateAnimalByTernaryOperator |        Cat | 2.490 ns |
| CreateAnimalByTernaryOperator |        Dog | 2.515 ns |
| CreateAnimalByTernaryOperator |     Parrot | 2.333 ns |
|------------------------------ |----------- |---------:|
|  CreateAnimalByIfElseOperator |        Cat | 2.368 ns |
|  CreateAnimalByIfElseOperator |        Dog | 2.545 ns |
|  CreateAnimalByIfElseOperator |     Parrot | 2.735 ns |
|------------------------------ |----------- |---------:|
|  CreateAnimalBySwitchOperator |        Cat | 2.747 ns |
|  CreateAnimalBySwitchOperator |        Dog | 2.730 ns |
|  CreateAnimalBySwitchOperator |     Parrot | 2.722 ns |


IL code
CreateAnimalByTernaryOperator(AnimalsFactory.AnimalType animalType)
{
    IL_0000: ldarg.0      // animalType
    IL_0001: brfalse.s    IL_0013
    IL_0003: ldarg.0      // animalType
    IL_0004: ldc.i4.1
    IL_0005: beq.s        IL_000d
    IL_0007: newobj       instance void AnimalsFactory.Parrot::.ctor()
    IL_000c: ret
    IL_000d: newobj       instance void AnimalsFactory.Dog::.ctor()
    IL_0012: ret
    IL_0013: newobj       instance void AnimalsFactory.Cat::.ctor()
    IL_0018: ret
}

CreateAnimalByIfElseOperator(AnimalsFactory.AnimalType animalType)
{
    IL_0000: ldarg.0      // animalType
    IL_0001: brtrue.s     IL_0009
    IL_0003: newobj       instance void AnimalsFactory.Cat::.ctor()
    IL_0008: ret
    IL_0009: ldarg.0      // animalType
    IL_000a: ldc.i4.1
    IL_000b: bne.un.s     IL_0013
    IL_000d: newobj       instance void AnimalsFactory.Dog::.ctor()
    IL_0012: ret
    IL_0013: newobj       instance void AnimalsFactory.Parrot::.ctor()
    IL_0018: ret

}

CreateOtherAnimalBySwitchOperator(AnimalsFactory.AnimalType animalType)
{
    IL_0000: ldarg.0      // animalType
    IL_0001: switch       (IL_0014, IL_001a, IL_0020)
    IL_0012: br.s         IL_0026
    IL_0014: newobj       instance void AnimalsFactory.Cat::.ctor()
    IL_0019: ret
    IL_001a: newobj       instance void AnimalsFactory.Dog::.ctor()
    IL_001f: ret
    IL_0020: newobj       instance void AnimalsFactory.Parrot::.ctor()
    IL_0025: ret
    IL_0026: newobj       instance void System.InvalidOperationException::.ctor()
    IL_002b: throw
}


Conclusion 1: For ternary and if-else statements, the runtime directly depends on the number of conditional jumps that have passed in the execution thread.

Conclusion 2: The C # switch statement is converted in IL code to a switch statement and, on average, works a little longer than regular branching statements.

Conclusion 3: Everything above is relevant only for the .NET Framework v.4.8. Having run the same tests on .NetCore, we got completely different results that have yet to be interpreted somehow.

Benchmarks .NetCore 3.0
|                          | AnimalType |     |
|------------------------------ |----------- |---------:|
| CreateAnimalByTernaryOperator |        Cat | 3.046 ns |
| CreateAnimalByTernaryOperator |        Dog | 2.984 ns |
| CreateAnimalByTernaryOperator |     Parrot | 3.019 ns |
|------------------------------ |----------- |---------:|
|  CreateAnimalByIfElseOperator |        Cat | 2.980 ns |
|  CreateAnimalByIfElseOperator |        Dog | 2.977 ns |
|  CreateAnimalByIfElseOperator |     Parrot | 3.103 ns |
|------------------------------ |----------- |---------:|
|  CreateAnimalBySwitchOperator |        Cat | 3.519 ns |
|  CreateAnimalBySwitchOperator |        Dog | 3.533 ns |
|  CreateAnimalBySwitchOperator |     Parrot | 3.312 ns |


Processor: Intel® Core (TM) i7-7700K

Sources

All Articles