From 67f753d27f619e8399320fdea8a543dfde5390df Mon Sep 17 00:00:00 2001 From: Max Horn Date: Mon, 5 Feb 2018 16:22:54 +0100 Subject: [PATCH] kernel: optimize LcmInt for small inputs This gives a tiny speed boost, but more importantly, avoids unnecessary allocation of temporary memory Before: gap> for i in [1..10000000] do LcmInt(2^50*13, 2^50*17); od; time; 2605 After: gap> for i in [1..10000000] do LcmInt(2^50*13, 2^50*17); od; time; 2427 --- src/integer.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/integer.c b/src/integer.c index fb863df8789..19bcb1b1d04 100644 --- a/src/integer.c +++ b/src/integer.c @@ -2202,6 +2202,15 @@ Obj LcmInt(Obj opL, Obj opR) sizeL = SIZE_INT_OR_INTOBJ(opL); sizeR = SIZE_INT_OR_INTOBJ(opR); + if (sizeL == 1 || sizeR == 1) { + if (sizeR != 1) { + SWAP(Obj, opL, opR); + } + Obj gcd = GcdInt(opL, opR); + opR = QuoInt(opR, gcd); + return AbsInt(ProdInt(opL, opR)); + } + NEW_FAKEMPZ(mpzResult, sizeL + sizeR); FAKEMPZ_GMPorINTOBJ(mpzL, opL); FAKEMPZ_GMPorINTOBJ(mpzR, opR);