2 ms·
> The strongest argument seems to be about performance It may be an argument, but it's not a strong one. If the improved code can be written like the author pu
by aktenlage 3y ago
> The strongest argument seems to be about performance
It may be an argument, but it's not a strong one. If the improved code can be written like the author puts it in their example (see below), the condition is constant over the runtime of the loop. So unless you evaluate an expensive condition every time, you are good. Branch prediction will have your back.
If condition is just a boolean expression using const values, I'd even guess the compiler will figure it out.
if condition {
for walrus in walruses {
walrus.frobnicate()
}
} else {
for walrus in walruses {
walrus.transmogrify()
}
}
Branch prediction should have you covered here. If you can easily rewrite it in