Skip to main content

Implement faster divmod() for moduli with trailing 0 bits

Project description

Info:

This is the README file for ShiftDivMod.

Author:

Shlomi Fish <shlomif@cpan.org>

Date:
2020-09-13
Version:
0.2.0
https://travis-ci.org/shlomif/shift_divmod.svg?branch=master

PURPOSE

This distribution implements faster divmod() (and .mod()) operations for moduli with a large number of trailing 0 bits (where the div/mod base is divisible by 2 ** n for an integer n).

It should yield the same result as the built-n divmod() function for positive numerators (its behaviour for negative ones is currently untested and undefined).

INSTALLATION

pip3 install shift_divmod

USAGE

from shift_divmod import ShiftDivMod

base = 997
shift = 1200
modder = ShiftDivMod(base, shift)
# Alternative constructor which may require more
# work and eventualy calls the default constructor
modder = ShiftDivMod.from_int(base << shift)

x = 10 ** 500
# Same as divmod(x, (base << shift))
print( modder.divmod(x) )

NOTES

The code from which this distribution has been derived, was proposed as a proof-of-concept for a potential improvement for the built in cpython3 operations here: https://bugs.python.org/issue41487 . However, changing cpython3 in this manner was rejected.

libdivide ( https://github.com/ridiculousfish/libdivide ) provides a different, but also interesting, approach for optimizing division.

BENCHMARKS:

On my system, I got these results after running python3 code/examples/shift_divmod_example.py bench (reformated for clarity):

{'val': 5206685, 'time': 38.86349368095398, 'reached': 1000,
 'interrupted': False, 'mode': 'gen_shift_mod'}
{'val': 5206685, 'time': 39.018390417099, 'reached': 1000,
 'interrupted': False, 'mode': 'shiftmodpre'}
{'val': mpz(5206685), 'time': 167.4433994293213, 'reached': 1000,
 'interrupted': False, 'mode': 'gmpy'}
{'val': 3346424, 'time': 229.94409656524658, 'reached': 25,
 'interrupted': True, 'mode': 'builtinops'}

System:    Kernel: 5.8.8-200.fc32.x86_64 x86_64 bits: 64
    Desktop: KDE Plasma 5.18.5
           Distro: Fedora release 32 (Thirty Two)
CPU:       Info: Quad Core model: Intel Core i5-8259U
    bits: 64 type: MT MCP L2 cache: 6144 KiB
           Speed: 1600 MHz min/max: 400/3800 MHz Core speeds (MHz):
                1: 1600 2: 1600 3: 1601
           4: 1600 5: 1600 6: 1601 7: 1601 8: 1601
Graphics:  Device-1: Intel Iris Plus Graphics 655 driver: i915 v: kernel
           Display: server: Fedora Project
           X.org 1.20.8 driver: modesetting unloaded: fbdev,vesa
           resolution: 1920x1080~60Hz
           OpenGL: renderer: Mesa Intel Iris Plus
           Graphics 655 (CFL GT3) v: 4.6 Mesa 20.1.7

As can be noticed the shift_divmod based versions are over 4 times faster than GMP and much faster than the builtinops which only completed 25 out of 1,000 iterations before being interrupted. Note that for that use case, using GMP’s modular exponentiation seems even faster.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

shift_divmod-0.2.0.tar.gz (13.4 kB view details)

Uploaded Source

File details

Details for the file shift_divmod-0.2.0.tar.gz.

File metadata

  • Download URL: shift_divmod-0.2.0.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.5

File hashes

Hashes for shift_divmod-0.2.0.tar.gz
Algorithm Hash digest
SHA256 b6c7a2e292ee90ba4bb8c6917fa3e3d0e5a54bae047cc18ddb81ff88ae9feb43
MD5 9b442d7bb7cd54170298f5b4317c7db6
BLAKE2b-256 68c2203495e2b46dfb8ab1601208a633c748d59630e2c4dcb9a9091b9099d646

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page