Comments (11)
It will be awesome, I currently cast mpz to python int to get bytes: int(x).to_bytes(32,'big') and it is pretty slow...
from gmpy.
from gmpy.
BTW, here some simple benchmarks (including my draft patch for this bug):
$ python -m timeit -s 'from math import factorial' -s 'a = factorial(57)' 'a.to_bytes(32)'
1000000 loops, best of 5: 281 nsec per loop
$ python -m timeit -s 'from gmpy2 import fac' -s 'a = fac(57)' 'a.to_bytes(32)'
500000 loops, best of 5: 772 nsec per loop
$ python -m timeit -s 'from gmpy2 import fac' -s 'a = fac(57)' 'int(a).to_bytes(32)'
500000 loops, best of 5: 863 nsec per loop
$ python -m timeit -s 'from gmpy2 import fac, to_binary' -s 'a = fac(57)' 'to_binary(a)'
500000 loops, best of 5: 444 nsec per loop
from gmpy.
For clarification, to_binary
converts all gmpy2
types such that they can be recovered by from_binary
. It should be (somewhat) platform neutral. 64-bit systems should be able to read binary blobs created on 32-bit system and vice-versa.
I'll review the pull request.
from gmpy.
@skirpichev, yes but to_bytes() and to_binary() have different meaning.
to_binary() takes care of a portable import/export and so adds a header. It needs a lot of operations to get a result similar to the python int.to_bytes()
Why mpz.to_bytes() is so slow vs int.to_bytes() ?
from gmpy.
Why mpz.to_bytes() is so slow vs int.to_bytes() ?
For above tests it seems to be connected with the argument parsing and (?) METH_FASTCALL calling convention for int.to_bytes (I'll try to build the gmpy2 with this patch for the v3.6 of CPython to verify that.).
from gmpy.
Here is v3.6 on same host:
$ python -m timeit -s 'from math import factorial' -s 'a = factorial(57)' 'a.to_bytes(32, "big")'
1000000 loops, best of 3: 0.776 usec per loop
from gmpy.
Wow, METH_FASTCALL worth it!
from gmpy.
Some comments on METH_FASTCALL. I converted _mpmath_normalize to use the vectorcall protocol. _mpmath_normalize requires exactly 6 positional arguments. There are no keyword arguments. METH_FASTCALL decreased the function call overhear by ~30ns.
to_bytes/from_bytes take keyword arguments so the savings may be better. I plan to do some experimentation this weekend.
from gmpy.
from gmpy.
Here is an initial version with METH_FASTCALL:
https://github.com/skirpichev/gmpy/tree/fix-357
$ python -m timeit -s 'from math import factorial' -s 'a = factorial(57)' 'a.to_bytes(32)'
1000000 loops, best of 5: 276 nsec per loop
$ python -m timeit -s 'from gmpy2 import fac' -s 'a = fac(57)' 'a.to_bytes(32)'
500000 loops, best of 5: 485 nsec per loop
$ python -m timeit -s 'from gmpy2 import fac' -s 'a = fac(57)' 'int(a).to_bytes(32)'
500000 loops, best of 5: 810 nsec per loop
from gmpy.
Related Issues (20)
- Dead paper link in comments
- 2.2.0: change in tagging convention? ๐ค HOT 8
- Abnormal situation of converting "mpz" to "str" HOT 4
- gmpy2 Sieve Example Code Problem HOT 10
- Add .bit_count() to xmpz type
- Type declarations to allow integration with type checking and linting tools HOT 2
- Probably incorrect error message. HOT 18
- Invalid mpq.__float__? HOT 5
- Intentional fallthroughs?
- Intentional fallthroughs? HOT 2
- format() should take into account rounding mode of the context?
- Incorrect signature for gmpy2.digits() in docs
- "Invalid conversion specification" error for align '=' value in format spec HOT 1
- Change defaults for format-style printing? HOT 1
- Conversion from mpz to numpy longdouble is incorrect HOT 10
- support in using gmpy2 HOT 6
- gmpy2 does not support trillion-digit Pi calculations. HOT 6
- Memory leak in gmpy2_cache HOT 9
- Memory leak when using gmpy2.ieee context manager HOT 3
- Access to abs() from C interface HOT 1
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.