Comments (2)
I tried three tests.
$ py311 -m timeit -r10 -s 'from gmpy2 import fac as f, mpz as z;from pickle import dumps as d' -s 'a=f(z(1000))' 'd(a)'
50000 loops, best of 10: 4.92 usec per loop
$ py311 -m timeit -r10 -s 'from math import factorial as f;from pickle import dumps as d' -s 'a=f(1000)' 'd(a)'
200000 loops, best of 10: 1.3 usec per loop
$ py311 -m timeit -r10 -s 'from gmpy2 import fac as f, mpz as z, to_binary as d' -s 'a=f(z(1000))' 'd(a)'
100000 loops, best of 10: 3.23 usec per loop
The third test directly calls gmpy2.to_binary
which creates the pickled data. to_binary
calls mpz_export
for all the data manipulation.
The Python code inside gmpy2 is ancient. I would accept removing it since we no longer need to support old versions of Python and mpz
is now a real type and not a factory function.
I don't know if it is worth trying to improve on mpz_export/mpz_import
? The code in mpz_pylong.c
may help.
from gmpy.
BTW, the mpz.to_bytes() (or to_binary) is systematically slower than int.to_bytes(). Here is a simple benchmark:
Code:
# a.py
from gmpy2 import mpz, to_binary
import sys
import random
import time
import platform
import matplotlib.pyplot as plt
int_time = []
mpz_time = []
#bin_time = []
times = 15
nbits = int(float(sys.argv[1]))
random.seed(1)
r = sorted(random.sample(range(1, nbits), 500))
for k in r:
ns = [random.randint(2**k//2, 2**k) for _ in range(times)]
nl = [(n, (n.bit_length() + 7)//8 + 2) for n in ns]
start = time.perf_counter_ns()
for n, l in nl:
n.to_bytes(l)
int_time.append((time.perf_counter_ns() - start) / times)
nl = [(mpz(n), l) for n, l in nl]
start = time.perf_counter_ns()
for n, l in nl:
n.to_bytes(l)
mpz_time.append((time.perf_counter_ns() - start) / times)
# start = time.perf_counter_ns()
# for n, l in nl:
# to_binary(n)
# bin_time.append((time.perf_counter_ns() - start) / times)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(r, int_time, label='int.to_bytes()')
ax.plot(r, mpz_time, label='mpz.to_bytes()')
#ax.plot(r, bin_time, label='to_binary(mpz)')
ax.set_yscale('log')
ax.set_xlabel('bits')
ax.set_ylabel('time (ns)')
ax.legend()
plt.title('Benchmark for to_bytes with ' + str(nbits) + ' bits.')
plt.show()
fig.savefig('to_bytes-'+str(nbits) + '.png')
UPD:
$ python
Python 3.11.3+ (heads/3.11:9fbb614c4e, Apr 29 2023, 14:18:05) [GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import gmpy2, sys
>>> gmpy2.mp_limbsize()
64
>>> sys.int_info
sys.int_info(bits_per_digit=30, sizeof_digit=4, default_max_str_digits=4300, str_digits_check_threshold=640)
from gmpy.
Related Issues (20)
- New version? 🤔 HOT 15
- 2.1.5: test suite fails with errors `invalid option: '+SKIP_MPC_LESS_THAN_110'` HOT 9
- 2.1.5: *.h and *.pxd files are installed HOT 5
- Is the `is_strong_bpsw_prp` code correct? HOT 2
- Assessment of the difficulty in porting CPU architecture for the gmpy
- Description of `is_euler_prp` HOT 1
- mpz object missing to_bytes() method HOT 4
- complex conjugate crash HOT 2
- Linux wheels in the latest release (2.2.0a1) are broken HOT 10
- What is the paper that `is_extra_strong_lucas_prp` is referring to? HOT 4
- About the case where `k=0` is input to `lucasu_mod` and `lucasv_mod` HOT 2
- gmpy-2.1.5 fails to compile with python-3.12 HOT 10
- cython C-API is broken in published wheels (and in the build_wheels.yml CI workflow) HOT 9
- Pip install build fails, can't find mpc.h HOT 6
- Deprecate local_context()? HOT 18
- Impending release of gmpy2 2.2.0a2 HOT 10
- Issue with large factorial HOT 6
- Access to gmplib.org
- Alternatives to using private API for conversion to/from CPython int's HOT 1
- Build & tests wheels for M$ Windows
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gmpy.