Re:I 10x'd a TI-84 emulator's speed by replacing a switch-case
by firloop on Monday August 08, 2022 @02:51AM
(#32382974)
Wonder if that benchmark still holds true for Golang. Go 1.19 (released last week) includes jump table optimizations for switch statements.
Re:I 10x'd a TI-84 emulator's speed by replacing a switch-case
by jheuel on Monday August 08, 2022 @04:28AM
(#32383420)
I just tested it:
1.18:
BenchmarkMap-10 13388006 77.38 ns/op
BenchmarkSwitch-10 50482033 23.00 ns/op
BenchmarkSlice-10 100000000 10.90 ns/op
1.19: BenchmarkMap-10 14947908 76.78 ns/op
BenchmarkSwitch-10 49444435 23.01 ns/op
BenchmarkSlice-10 100000000 10.74 ns/op
edit: If I understand the release notes correctly the switch optimisations are only new for large integers and strings.
E.g, it doesn't already work in Go, https://stackoverflow.com/questions/46789259/map-vs-switch-p...
But if there are too many switch-cases, the codes may be ugly to read, so maybe using a map or array is a better choice here.