ncw / gmp

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

Why is the running speed of GMP here much slower than the built-in big library of GO ? #16

Open 1853582 opened 1 year ago

1853582 commented 1 year ago

When I tested this GMP library, I found that its computational speed was much slower in my example than the built-in large data library of Go itself. Why is this? I am here to calculate the inner product composed of two arrays of large numbers, and then calculate many inner products.

1853582 commented 1 year ago

0YBK_}U0Y$DX7)UG1U$ )AB ({PMPHV50IL`ANGJKZCVIRW N OX23{O(L` %)S448C_XW1 X7IZ5STH `7 CIFXDYFB3 H

1853582 commented 1 year ago

The previous one was the big database time of Go, while the latter one changed to GMP, which actually slowed down the time a lot. ZP}T}766( 9O3787 U4(1GP B`(6G45E9OM(N 6 INP81R5

ncw commented 1 year ago

The problem with gmp is that to call it requires switching to C calling conventions.

This article suggests that a normal function call in Go takes approx 1.8 nS whereas a call to a C function takes approx 170 nS.

The integers you are using are quite small so that overhead of 170 nS is much larger than the time the arithmetic operation takes.

This library really only comes into its own when using >100 digit or maybe 1,000 digit numbers.

What size are the integers you are using?

1853582 commented 1 year ago

The number I use is an integer in the finite field Zp, p = 2 ^ 128-51

ncw commented 1 year ago

So a 128 bit number. I'd say that the CGO interface speed will negate any speed savings you might get from GMP here :-(