That may have been a mistake...
-
Hi, I know you are not totally serious but what is the correct way and why should it not be faster than a large switch? I think the created IL code behaves similar to a very large if-then-elseif construct. Thus the switch-solution will get slower if you increase the range of supported numbers. btw: I would create a "real algorithm" to produce the strings and pair it with a dictionary as a cache. Robert
Robert Rohde wrote:
I think the created IL code behaves similar to a very large if-then-elseif construct. Thus the switch-solution will get slower if you increase the range of supported numbers.
Wrong. IL has a special instruction for switch which the JIT translates into a jump table. Switches of consecutive integer values (so that a table without large holes is possible) don't get slower if you add cases. Switches of strings are implemented using a Dictionary<string, int> and the IL switch instruction on the resulting int.
-
As some of you may know, I created a Tip/Trick recently about converting numbers to the word equivalent (523 to "Five hundred and twenty three"). With my tongue firmly rammed into my cheek, I suggested that a switch statement would be a good way to do it. Well, it's Saturday, and I thought to myself "A switch has got to be faster than the proper way - I wonder how much faster?" So I thought I'd check. I wrote and tested a proper version and used it to generate the Big Switch code I'd need, writing it to a text file "BigSwitch.cs". Then I could include this and do a side-by-side comparison. Initial testing of the "proper" way to do it suggested that one million iterations should test all code paths and eliminate cached jitter in the results. So, I made my mistake. I double clicked on the "BigSwitch.cs" file I had created. VS2008 started to load it. I'm still not sure if it would have suceeded; I killed the process after 30 minutes. Note to self: Do not attempt to load 82.7 Mb code fragments into VS again... :-O
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
-
Done - Conversion to Roman dates using a database[^] Thank you for the idea! :laugh:
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.