From 6391c4f46156240fe36c08e00449a5833f8b65c4 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; 2655 gap> for i in [1..10000000] do LcmInt(2^60*13, 2^60*17); od; time; 4450 gap> for i in [1..10000000] do LcmInt(2^70*13, 2^70*17); od; time; 5831 After: gap> for i in [1..10000000] do LcmInt(2^50*13, 2^50*17); od; time; 2381 gap> for i in [1..10000000] do LcmInt(2^60*13, 2^60*17); od; time; 4505 gap> for i in [1..10000000] do LcmInt(2^70*13, 2^70*17); od; time; 5854 --- src/integer.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/integer.c b/src/integer.c index fb863df878..4a98a5709f 100644 --- a/src/integer.c +++ b/src/integer.c @@ -2199,6 +2199,15 @@ Obj LcmInt(Obj opL, Obj opR) if (opL == INTOBJ_INT(0) || opR == INTOBJ_INT(0)) return INTOBJ_INT(0); + if (IS_INTOBJ(opL) || IS_INTOBJ(opR)) { + if (!IS_INTOBJ(opR)) { + SWAP(Obj, opL, opR); + } + Obj gcd = GcdInt(opL, opR); + opR = QuoInt(opR, gcd); + return AbsInt(ProdInt(opL, opR)); + } + sizeL = SIZE_INT_OR_INTOBJ(opL); sizeR = SIZE_INT_OR_INTOBJ(opR);