ncw / gmp

Go language interface to GMP - GNU Multiprecision Library (golang)
BSD 3-Clause "New" or "Revised" License
117 stars 26 forks source link

gmp lib is slow than math/big #3

Closed niniwzw closed 7 years ago

niniwzw commented 8 years ago

BenchmarkGmpAdd1 5000000 252 ns/op BenchmarkGmpAdd10 5000000 251 ns/op BenchmarkGmpAdd100 5000000 262 ns/op BenchmarkGmpAdd1000 5000000 359 ns/op BenchmarkGmpAdd10000 1000000 1258 ns/op BenchmarkGmpAdd100000 200000 9997 ns/op BenchmarkGmpAdd1000000 10000 111326 ns/op BenchmarkGmpMul1 5000000 258 ns/op BenchmarkGmpMul10 5000000 255 ns/op BenchmarkGmpMul100 3000000 427 ns/op BenchmarkGmpMul1000 200000 8186 ns/op BenchmarkGmpMul10000 5000 227503 ns/op BenchmarkGmpMul100000 300 4796916 ns/op BenchmarkGmpMul1000000 20 70398239 ns/op BenchmarkBitset 5000000 239 ns/op BenchmarkBitsetNeg 5000000 238 ns/op BenchmarkBitsetOrig 1000000 2343 ns/op BenchmarkBitsetNegOrig 300000 4454 ns/op BenchmarkMathBigAdd1 30000000 42.8 ns/op BenchmarkMathBigAdd10 30000000 43.2 ns/op BenchmarkMathBigAdd100 30000000 46.6 ns/op BenchmarkMathBigAdd1000 20000000 84.2 ns/op BenchmarkMathBigAdd10000 3000000 518 ns/op BenchmarkMathBigAdd100000 300000 4966 ns/op BenchmarkMathBigAdd1000000 30000 59912 ns/op BenchmarkMathBigMul1 30000000 47.0 ns/op BenchmarkMathBigMul10 30000000 47.3 ns/op BenchmarkMathBigMul100 10000000 168 ns/op BenchmarkMathBigMul1000 300000 4821 ns/op BenchmarkMathBigMul10000 10000 220470 ns/op BenchmarkMathBigMul100000 200 8219446 ns/op BenchmarkMathBigMul1000000 5 308678773 ns/op

this is my test result

ncw commented 8 years ago

There is quite a big overhead (about 250 nS according to the benchmarks) in calling from Go to C so you only start winning with gmp for operations that take longer than that.

Taking the large multiply from your benchmarks

BenchmarkGmpMul1000000     20   70398239 ns/op
BenchmarkMathBigMul1000000  5 308678773 ns/op

So you can see GMP is much faster for this benchmark.

If you are dealing exclusively with small integers doing simple operations then this overhead will cause GMP to be slower than math/big.

It does depend on which operations you are doing though - Exp for instance is much faster with GMP even run on smallish integers (the length you might use for RSA for instance).

lemire commented 7 years ago

@ncw I think one would need to qualify your statement "GMP is very much faster than Go's math/big". Either you mean that the C library is much faster... but then it is not 100% clear that it is what you mean... or you mean that your wrapper is much faster than Go's math/big... then that's only true with a qualifier. Or, to keep things simple, you could say that your library can be much faster than Go's math/big.

ncw commented 7 years ago

@lemire You are correct of course. Performance isn't a simple linear scale of slower to faster - there are a lot of extra dimensions. I think it is true to say that this library can be faster than math/big so I should probably tone down the marketing speak in the README to that ;-)

niniwzw commented 7 years ago

3q @ncw @lemire