4 ms·
This optimization changes factorial from: 5 * (4 * (3 * (2 * 1))) to: (((1 * 5) * 4) * 3) * 2 It relies on the commutative and associative nature of integer
by Peaker 9y ago
This optimization changes factorial from:
5 * (4 * (3 * (2 * 1)))
to:
(((1 * 5) * 4) * 3) * 2
It relies on the commutative and associative nature of integer multiplication.
This means it's quite a brittle optimization -- it shouldn't really work for floats, for example, which aren't associative at multiplication. And it wouldn't work on any operation for which the compiler does not know the associativity.
So while it seems general -- it's quite specific and not widely applicable.