From bc11e5796220a69041dc4bbf7cd7db42bbe01cb1 Mon Sep 17 00:00:00 2001 From: nicoo Date: Sun, 24 May 2020 15:41:23 +0200 Subject: [PATCH] factor::factor: Use u64::trailing_zero instead of iterated division No significant performance impact (most of the time is spent elsewhere), but an easy and satisfying fix nevertheless. --- src/uu/factor/src/factor.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/uu/factor/src/factor.rs b/src/uu/factor/src/factor.rs index f01cae76d..894c61c81 100644 --- a/src/uu/factor/src/factor.rs +++ b/src/uu/factor/src/factor.rs @@ -83,9 +83,10 @@ fn factor(mut n: u64) -> Factors { return factors; } - while n % 2 == 0 { - n /= 2; - factors.push(2); + let z = n.trailing_zeros(); + if z > 0 { + factors.add(2, z as u8); + n = n >> z; } if n == 1 {