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;
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
  • Loading branch information
fingolfin committed Feb 7, 2018
1 parent 05123b6 commit 6391c4f
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 @@ -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);

Expand Down

0 comments on commit 6391c4f

Please sign in to comment.