Do the operator's instructions warrant the operator?

I've read in various places that switch

case statements string

can potentially be optimized (by the compiler or JIT) into hash tables for better performance. Hash tables with imperfect hashing functions obviously don't guarantee the order of things, so I was wondering:

  • Can a C # or JIT compiler do an optimization that converts a statement switch

    to a hash table for consistent performance?
  • Follow instructions switch

    in C #to case

    be ordered from top to bottom?
+3


source to share


1 answer


No, there is no guarantee that the order will be preserved since this is a purely compiler-based implementation, so even if that were true now, it .net 5.1

might be wrong for (say).

Design

switch/case

designed to identify unique options between the various available. So the order doesn't matter if not from a performance standpoint, but even there, it basically doesn't matter, and if not, it couldn't be predicted for the reasons described above.



So just ignore it and look at other parts of your program if you're looking for some performance bottleneck.

+3


source







All Articles