Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize to_binary() function and to_bytes() method
It seems, that using mpn_get_str() is more efficient than generic mpz_export(). Some benchmarks are here: #404 (comment) Not sure what else we can do for #404. In the python-gmp I've added also the `__reduce__` dunded method. This seems slightly better than rely on copyreg to support pickling: | Benchmark | ref | patch | gmp | |----------------|:-------:|:---------------------:|:---------------------:| | dumps(1<<7) | 23.9 us | 23.8 us: 1.01x faster | 22.6 us: 1.06x faster | | dumps(1<<38) | 24.0 us | 23.9 us: 1.01x faster | 22.7 us: 1.06x faster | | dumps(1<<300) | 24.1 us | 23.8 us: 1.01x faster | 22.9 us: 1.05x faster | | dumps(1<<3000) | 26.8 us | 25.2 us: 1.07x faster | 23.8 us: 1.13x faster | | Geometric mean | (ref) | 1.02x faster | 1.07x faster | Can we add pickling to the gmpy2 with even less overhead? I don't know. But if we avoid pickle machinery, you can see noticeable performance boost for small numbers too: | Benchmark | to_binary-ref | to_binary-patch | |----------------|:-------------:|:---------------------:| | dumps(1<<7) | 323 ns | 300 ns: 1.08x faster | | dumps(1<<38) | 352 ns | 315 ns: 1.12x faster | | dumps(1<<300) | 603 ns | 436 ns: 1.39x faster | | dumps(1<<3000) | 3.17 us | 1.57 us: 2.02x faster | | Geometric mean | (ref) | 1.35x faster | New code seems faster than int.to_bytes() roughly from 500bit numbers on my system.
- Loading branch information