Skip to content

Commit

Permalink
kernel: optimize LcmInt for small inputs
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fingolfin committed Feb 5, 2018
1 parent ea95324 commit 67f753d
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/integer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 67f753d

Please sign in to comment.