Git Product home page Git Product logo

Comments (11)

sfornengo avatar sfornengo commented on September 26, 2024

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.

skirpichev avatar skirpichev commented on September 26, 2024

from gmpy.

skirpichev avatar skirpichev commented on September 26, 2024

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.

casevh avatar casevh commented on September 26, 2024

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.

sfornengo avatar sfornengo commented on September 26, 2024

@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.

skirpichev avatar skirpichev commented on September 26, 2024

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.

skirpichev avatar skirpichev commented on September 26, 2024

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.

sfornengo avatar sfornengo commented on September 26, 2024

Wow, METH_FASTCALL worth it!

from gmpy.

casevh avatar casevh commented on September 26, 2024

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.

skirpichev avatar skirpichev commented on September 26, 2024

from gmpy.

skirpichev avatar skirpichev commented on September 26, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.