3

I understand that GMPY2 supports the GMP library and numpy has fast numerical libraries. I want to know how the speed compares to actually writing C (or C++) code with GMP. Since Python is a scripting language, I don't think it will ever be as fast as a compiled language, however I have been wrong about these generalizations before.

I can't get GMP to work on my computer, so I can't run any tests. If I could, just general math like addition and maybe some trig functions. I'll figure out GMP later.

qwr
  • 9,525
  • 5
  • 58
  • 102

1 Answers1

8

numpy and GMPY2 have different purposes.

numpy has fast numerical libraries but to achieve high performance, numpy is effectively restricted to working with vectors or arrays of low-level types - 16, 32, or 64 bit integers, or 32 or 64 bit floating point values. For example, numpy access highly optimized routines written in C (or Fortran) for performing matrix multiplication.

GMPY2 uses the GMP, MPFR, and MPC libraries for multiple-precision calculations. It isn't targeted towards vector or matrix operations.

The Python interpreter adds overhead to each call to an external library. Whether or not the slowdown is significant depends on the how much time is spend by the external library. If the running time of the external library is very short, say 10e-8 seconds, then Python's overhead is significant. If the running time of the external library is relatively long, several seconds or longer, then Python's overhead is probably insignificant.

Since you haven't said what you are trying to accomplish, I can't give a better answer.

Disclaimer: I maintain GMPY2.

casevh
  • 11,093
  • 1
  • 24
  • 35
  • I want to take a range of numbers (say integers) and perform lots of basic arithmetic, conditional statements, trig functions, and square roots. I think numpy would be a good choice, what do you think? – qwr May 30 '14 at 18:56
  • I don't know if numpy would be a good choice for you or not. You haven't told us what you really want to do. – casevh Jun 01 '14 at 04:07
  • Does gmpy2 support all function like `mpz_powm_sec`? – kelalaka Jan 25 '22 at 00:14
  • 1
    gmpy2 doesn't yet support `mpz_powm_sec` but it is planned. The MPIR fork of GMP doesn't support the constant time *_sec functions. I think SageMath still relies on MPIR so I need to determine the best course of action. Please open an issue at https://github.com/aleaxit/gmpy if you want to discuss this further. – casevh Jan 25 '22 at 08:22
  • @casevh I love your library btw! I wonder if you have benchmarked gmpy2 against any of the corresponding functions in Mathematica (which I think uses gmp in places too)? – M.R. Aug 05 '22 at 17:08