4 ms·
I sometimes wish that x * y would short circuit on x = 0. Just because you could.
by psuter 13y ago
I sometimes wish that x * y would short circuit on x = 0. Just because you could.
- valleyer 13y agoYou mean, like, in the compiler? If x is actually a macro that expands to zero, that will (or at least should, for a sufficiently smart compiler) happen. Otherwise, I can’t imagine a runtime check of this being appreciably faster than hardware multiply. Right?
- lubomir 13y agoI would guess he/she meant something along the lines of x * some_really_complicated_expression. If x is 0, you would not need to evaluate the other operand at all. It may even be a faster, but I doubt any compiler does this sort of optimization, since it is not safe in languages with non-pure functions. The complicated expression could change some global state or perform input/output. E.g. in Haskell, it would be possible to do this safely, because it is guaranteed by the type system that there are no side effects. However, even with literal 0 in the code the other operand still gets evaluated.
- valleyer 13y agoAh OK. That makes sense.