I am working on a python script that basically uses very long mpz integers (from gmpy2 library) and some small lists. Apart from other typical "pythonic" code optimizations, I have tried to reduce the times required for the calculations inside the script (basically, they are "mul", "add", "sub" calculations from gmpy2) by using the set_cache function as well, but even setting it to the max, i.e. set_cache(1000,16384), I can not see any difference between using or not using it, the timings are the same ones for big integers.
If somebody has experience about the usage of get_cache or for which kind of operations is better to set it, I would appreciate some information. I just know what the official gmpy page says about it (very basic), and googling about that method in Internet does not provide any interesting samples (at least I did not find them yet)
I'm the maintainer of gmpy2.
The caching that is controlled by set_cache()
and get_cache()
decreases the time it takes to create a new mpz
instance. The improvement is only measurable for moderate sized integers. Once you start working with very large numbers, the running time of the computation is so much longer than the object creation time, the effects of caching are negligible.
If the caching algorithm didn't limit the size of the objects cached, you can also consume all the system memory. That was a problem with a very early alpha release.
The functions are primarily used for testing. Decreasing or disabling the cache may be useful for system with limited memory. Generally, there isn't much benefit in tuning the cache size.
Since you haven't provided any code or identified your operating system, I can only provide some generic suggestions for performance tuning.
*
, +
, and -
operators instead of the mul()
, add()
, and sub()
. The operators will have less overhead.long
and gmpy2's mpz
types.gmpy2
for your specific processor. Unfortunately, this can be challenging on Windows.