I need to manipulate large numbers in Python that fit into 64 bits. Currently, my code is running on a 64-bit platform but there is small but distinct possibility that it will have to run on a 32-bit platform. Consequently, I would prefer to use long
type to represent my numbers. I understand there is a performance impact for using long
over int
type. How bad is it? I'll be performing a lot of divisions and multiplications on them, but the results should all fit into 64 bits, too.
If you're going to be doing a lot of heavy number crunching, have a look at "numpy".
If your program does a lot of numerical computations - to a point that performance matters, you should profile it, and have the numerical part running in native code. You should not have to worry if internally the numbers are Python "integers" or "long" - so much that Python 3 removes the type difference.
There are several approaches for it, from using numpy, cython, a C extension, running your program using pypy instead of the standard cpython, and even take a look at corepy - what you should not do is to have a numeric intensive task running in pure python if performance is an issue there. Event he most complicated of these - creating a C extension in the form of a single function that just perform the calculations is simple enough to be well worth the performance gains in this case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With