I just want to write the fastest possible Java code. Essentially I am trying to map midi pitch note values to the proper places on the treble/ bass cleffs. For example, the note middle C (is midi value 60) and is on the treble cleff at position -2. E (midi note 64) is at treble cleff position 0., etc. There will be about 20 mappings per cleff. So I will need to either put the 20 values in a HashMap, or construct a switch statement with the 20 values. Which one would be faster?
Actually, I just thought of something that would be the fastest possible solution. Create a huge array (or List) that is indexed by the midi note value. note.get(60) would return Integer -2. Excellent! Sometimes just posting an issue can help (regardless if anyone replies)
I would suggest using a HashMap actually as that is cleaner. You can then create a little wrapper class that can get the conversions for you and you can place static Strings in there for the keys to reference the values back out.
My feeling says a switch would be the fastest for 20 values. Even better than an array if that array has to be significantly larger than 20. For very large count of values the HashMap would be the best. Best looking (design wise) would probably be the HashMap…
Thanks for the replies. Keep in mind that i don't have to loop through the array or List. (I'll know the midi note value that was played, and can do a quick lookup -via an index to the array/List) If the situation was different, and I did have to loop through the List, then I would probably choose a HashMap, or switch instead. I guess the array sacrifices a little bit of memory for speed. (since not all the indexes will be used) It, however, would be nice to know if Java has a fast switch implementation (or if the compiler just converts a switch to something like if-else statements).
Keep in mind that i don't have to loop through the array or List.
Sure. You might think that branching (switch) might be slow. But memory access will be slower! So your array will be faster for larger value counts, only. I never read nor investigated on the imlplementation of switch as it does not really matter - especially in your case. The hotspot jit is quite good in optimizing, I think you should rely on that.
it's generally a bad idea to try optimising stuff until you know where the bottlenecks are. It could well turn out that a completely different bit of code is the bottleneck and your choice of hashmap/switch makes no difference whatsoever.
The best choice until you profile your app and find out which bit is slow is probably to write the most readable code that you can.
Everything depends on the amount of data and the operations you need to perform.
There are currently many collection implementations optimized for large datasets, retrieval or insertion.
The best way is to stick with one implementation and if you find after profiling problems try
to swith to an implementation that is faster than the one you currently use.
bm212 and kman - exactly
Well, I guess I just can't help trying to optimize what doesn't need optimization. I'm the classic obsessive cumpulsive coder :oops: