HACKER Q&A
📣 avithedev

What Is More Compute Efficient Between Golang's If and Switch Statements


I am curious to know which between the two is more efficient and how they work under the hood.

Thanks in advance.


  👤 bjourne Accepted Answer ✓
You can find out for yourself using the Compiler Explorer (godbolt): https://godbolt.org/z/qrYGzMjWd Looks to me as if If is more efficient here.

👤 throwawayadvsec
it's unlikely that there is a difference

compilers are usually optimizing these kind of statements so they end up similar or identical

if you wanna be sure, make two loops running a few million times with random if else / switches and time them