Skip to main content

Knuth B Shuffle Algorithm

Destructive, In-place Shuffle

Reverse Order Random Swap Backwards - a cache friendly inplace shuffle algorithm.

Mac & Linux Installation

$ pip install KnuthB

Installation may require Cython, and modern C++ development environment (Clang or GCC).

Shuffle Tests

Base Case: Random.shuffle

>>> from random import py_shuffle
>>> a = [*range(10)]
>>> print(a)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> py_shuffle(a)
>>> print(a)
[4, 3, 2, 0, 9, 7, 5, 6, 8, 1]

Test Case: KnuthB.shuffle

>>> from KnuthB import shuffle as knuth
>>> a = [*range(10)]
>>> print(a)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> knuth(a)
>>> print(a)
[9, 1, 7, 8, 3, 2, 4, 6, 5, 0]

MonkeyScope Timer Tests

Base Case: Random.shuffle()

>>> from MonkeyScope import timer
>>> from random import shuffle as py_shuffle
>>> a = [*range(10000)]
>>> a = [*range(10000)]
>>> timer(py_shuffle, a, cycles=1)
Typical Timing: 10205985 ± 0 ns

Test Case: KnuthB.shuffle()

>>> from MonkeyScope import timer
>>> from KnuthB import shuffle as knuth
>>> a = [*range(10000)]
>>> timer(knuth, a, cycles=1)
Typical Timing: 679970 ± 0 ns

Performance Results

Time to Shuffle 10,000 Items

- Random.shuffle: 10,205,985 ns (approx 10.2 milliseconds)
- KnuthB.shuffle:    679,970 ns (approx  0.7 milliseconds)

    * Lower is better

Metadata

Release files for KnuthB 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for KnuthB 0.0.1
File Size Uploaded
KnuthB-0.0.1.tar.gz 22.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for KnuthB 0.0.1
File Interpreter ABI Platform
KnuthB-0.0.1-cp38-cp38-macosx_10_9_x86_64.whl CPython 3.8 CPython 3.8 macOS 10.9+ x86-64 Details

Total release size: 33.2 kB

Release files / KnuthB-0.0.1.tar.gz

Download URL KnuthB-0.0.1.tar.gz
Size 22.3 kB
Tags Source
SHA-256 checksum
How to use checksums
64f30ee9bccb6b1f17e5a39d29117119759a50aab83256d3c9e59497c19b8d6c
BLAKE2b-256 checksum
How to use checksums
8e58d3deb8245f3ddeff02d23273b4ae30bd86a584c5b70cfae33ae03d05b4b4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.0

Release files / KnuthB-0.0.1-cp38-cp38-macosx_10_9_x86_64.whl

Download URL KnuthB-0.0.1-cp38-cp38-macosx_10_9_x86_64.whl
Size 11.0 kB
Tags CPython 3.8 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
3eb5829e1d130d5f1d749a12dadcdbbf2d2bfb317eede1fb62428a610848dc2e
BLAKE2b-256 checksum
How to use checksums
7c3a2750a1029fca5b6695d69a1b62e72ecbcd45653fb3d94b0bb7cdb684e77b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.0

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page