0

For a C# simulation software module (3D mesh), I have a switch/case block consisting of 3^6 cases (3 conditions for 6 neighbours each). Along with the content, which is roughly the same size for each block, it results in about 9000 LoC.

Given the number of different conditions, I doubt that there is anything more performant than doing this with switch/case, and when running it, it's reasonably fast. However, on the first call, the programme freezes for several seconds. When repeating the calculations without restarting the software, it stays fast. This effect is the same for Debug and Release.

The accepted answer in Is "else if" faster than "switch() case"? states that switch/case is implemented through a hash list. Is this list really created at runtime? If so, can it be optimised somehow so it doesn't take several seconds at each launch? If not, where does the time delay come from? (I already checked that the delay is gone when commenting out the switch/case block.)

Any help or hints would be much appreciated!

Community
  • 1
  • 1
Brokenmind
  • 41
  • 3
  • 2
    See this answer http://stackoverflow.com/a/6417625/286976 – Gusdor Mar 19 '15 at 09:32
  • 2
    switch/case is **not** implemented as a hash list unless it is a switch on a string, and/or the candidate items are non-contiguous; normally, it is actually a direct multi-jump (the [`Switch`](https://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.switch(v=vs.110).aspx) opcode) – Marc Gravell Mar 19 '15 at 09:33
  • It sounds to be like this is just JIT/Fusion time... have you tried NGEN? – Marc Gravell Mar 19 '15 at 09:38
  • @Gusdor Great, adding this at startup gets rid of the delay somewhat! – Brokenmind Mar 19 '15 at 10:12
  • @MarcGravell I just tried to use it, but on my machine it doesn't output a new executable. I will look into it later, but it sure looks promising. – Brokenmind Mar 19 '15 at 10:13
  • @Brokenmind it isn't *meant* to output a new executable; rather, the CLR itself detects the NGen image. For the background storage location, see http://stackoverflow.com/questions/5657566/where-can-i-find-location-of-generated-file-after-doing-ngen – Marc Gravell Mar 19 '15 at 10:17
  • @Brokenmind bare in mind that you still have to pay the price of JITing the method. I'd only be bothered about this if i wanted to guarantee some documented execution response time. _"The classes in System.Runtime.CompilerServices are for compiler writers' use only."_ – Gusdor Mar 19 '15 at 11:36

0 Answers0