From 6eea8c5f307162c9d0004950bc3defa7cdbfe22c Mon Sep 17 00:00:00 2001 From: Roy Ivy III Date: Thu, 8 Oct 2020 18:58:09 -0500 Subject: [PATCH] perf/factor ~ improve factor() quotient and loop comparison (~6% time improvement) --- src/uu/factor/src/factor.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/uu/factor/src/factor.rs b/src/uu/factor/src/factor.rs index ffb099f2a..3cf9783a2 100644 --- a/src/uu/factor/src/factor.rs +++ b/src/uu/factor/src/factor.rs @@ -171,19 +171,19 @@ pub fn factor(num: u64) -> Factors { let mut gcd_queue = Decomposition::one(); let quotient = factor / divisor; - if quotient == divisor { + let mut trivial_gcd = quotient == divisor; + if trivial_gcd { gcd_queue.add(divisor, exp + 1); } else { gcd_queue.add(divisor, exp); gcd_queue.add(quotient, exp); } - let mut non_trivial_gcd = quotient != divisor; - while non_trivial_gcd { + while !trivial_gcd { debug_assert_eq!(factor, gcd_queue.product()); let mut tmp = Decomposition::one(); - non_trivial_gcd = false; + trivial_gcd = true; for i in 0..gcd_queue.0.len() - 1 { let (mut a, exp_a) = gcd_queue.0[i]; let (mut b, exp_b) = gcd_queue.0[i + 1]; @@ -194,7 +194,7 @@ pub fn factor(num: u64) -> Factors { let g = gcd(a, b); if g != 1 { - non_trivial_gcd = true; + trivial_gcd = false; a /= g; b /= g; }