3 ms·
What you want is something that compiles to conditional moves. A good compiler should compile your proposed version to the same machine code as the max & min me
by jacobolus 6y ago
What you want is something that compiles to conditional moves. A good compiler should compile your proposed version to the same machine code as the max & min method. If for one reason or another it compiles to branches, it’ll end up being slower.
In Javascript in my browser (Safari) when these methods get called enough to get compiled using the most aggressive stage of the JIT, they end up essentially the same speed. On my laptop either one runs about 145 million times per second on one CPU core.
I wouldn’t be surprised if a C compiler ended up making this function significantly faster, but I haven’t tested it.