General statistics
List of Youtube channels
Youtube commenter search
Distinguished comments
About
Anony Mousse
Low Level
comments
Comments by "Anony Mousse" (@anon_y_mousse) on "why are switch statements so HECKIN fast?" video.
Well, bash was written in C and somewhat influenced by it, but they are still significantly different.
2
I still remember reading the AMD optimization manual like 20-ish years ago and figuring out how to use that to your advantage in so many ways. Even when you have simple string constants you can still take advantage of it by pre-hashing the strings, and there was another trick in there involving multiple if branches that could be compressed into a single switch by using the compiler's own optimizations to convert a test into a simple bit and then bitwise or them all together.
2
Interesting viewpoint, but big O notation doesn't tell the whole story because something can have an O(1) complexity yet run slower than something that's O(N). Second of all, while compilers have gotten really good at optimizing, there will be cases where refactoring to use a switch will improve the code that the compiler generates. A single jump in the case of a switch as opposed to a whole slew of them in an if/else chain will be demonstrably better and it shouldn't even require testing.
1
@niirvashtzero That's true of a lot of things, though at -O0 a switch will always generate better, faster code. At -O3 it won't matter for most uses, but there are times when a bunch of if/else blocks won't be optimized as well by the compiler and it's better to get ahead of it, aside from the fact that it will be easier to read as a switch.
1
@Vaaaaadim Except that this is part of the optimization process, both in terms of performance and making your code easier to read and thus update.
1
Depends on the code, though an added consideration is that a switch will be easier to read than a mess of if/else blocks.
1
@sent4444 I can't directly link it, but just Duck the AMD optimization manual and you should find it. The Intel optimization manual is also a good resource. They both provide some really good tips if you're thinking of getting into writing assembly or a compiler or just improving your code from a higher level.
1