Skip to main content

C++ parallel sorting algorithms through Cython - up to 8 times faster than NumPy

Project description

C++ parallel sorting algorithms through Cython - up to 8 times faster than NumPy

pip install cythonfastsort

Tested against Windows / Python 3.11 / Anaconda

Cython (and a C/C++ compiler) must be installed to use the optimized Cython implementation.

import sys
import timeit
import numpy as np
from cythonfastsort import generate_random_arrays, sort_all


size = 10000000
arras = [
    (size, "float32", -555555555, 555555555),
    (size, "float64", -100000000000, 100000000000),
    (size, np.uint8, 0, 255),
    (size, np.int8, -120, 120),
    (size, np.int16, -30000, 30000),
    (size, np.int32, -555555555, 555555555),
    (size, np.int64, -(sys.maxsize-1)//2, (sys.maxsize-1)//2),
    (size, np.uint16, 0, 60000),
    (size, np.uint32, 0, 555555555),
    (size, np.uint64, 0, sys.maxsize-1),
]

reps = 3
for a in arras:
    arr = generate_random_arrays(*a)
    seq = generate_random_arrays(size // 10, *a[1:])
    s = """u=sort_all(arr)"""
    u = sort_all(arr)
    t1 = timeit.timeit(s, globals=globals(), number=reps) / reps
    print('c++ ', t1)
    s2 = """q=np.sort(arr)"""
    q = np.sort(arr)
    t2 = timeit.timeit(s2, globals=globals(), number=reps) / reps
    print('np ', t2)
    print(np.all(q == u))

    print("-----------------")

haystack = np.array(
    [
        b"Cumings",
        b"Heikkinen",
        b"Cumings, Mrs. John Bradley (Florence Briggs Thayer)",
        b"aaa",
        b"bbbb()",
        b"Futrelle",
        b"Allen",
        b"Cumings, Mrs. John Bradley (Florence Briggs Thayer)q",
        b"Braund, Mr. Owen Harris",
        b"Heikkinen, Miss. Laina",
        b"Futrelle, Mrs. Jacques Heath (Lily May Peel)",
        b"Allen, Mr. William Henry",
        b"Braund",
    ],
    dtype="S",
)

arr = np.ascontiguousarray(np.concatenate([haystack for _ in range(200000)]))
reps = 5
s = """u=sort_all(arr)"""
u = sort_all(arr)
t1 = timeit.timeit(s, globals=globals(), number=reps) / reps
s = """u=np.sort(arr)"""
q = np.sort(arr)
t2 = timeit.timeit(s, globals=globals(), number=reps) / reps
print('c++ ', t1)
print('np ', t2)
print(np.all(q == u))

# c++  0.2110750000004676
# np  0.6982908999998472
# True
# -----------------
# c++  0.22871500000110245
# np  0.7115663999999621
# True
# -----------------
# c++  0.0869510999994721
# np  0.27525126666781335
# True
# -----------------
# c++  0.09770773333366378
# np  0.28797130000020843
# True
# -----------------
# c++  0.13066273333364128
# np  0.47454773333326256
# True
# -----------------
# c++  0.17967379999997016
# np  0.6125306666666196
# True
# -----------------
# c++  0.19168066666679806
# np  0.6346349666673632
# True
# -----------------
# c++  0.06744649999988421
# np  0.47243033333264367
# True
# -----------------
# c++  0.09263499999966977
# np  0.6186357666665572
# True
# -----------------
# c++  0.11886440000065097
# np  0.6260395666661983
# True
# -----------------
# c++  0.9570277200000419
# np  1.4337052399998356
# True

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

cythonfastsort-0.10.tar.gz (23.3 kB view details)

Uploaded Source

Built Distribution

cythonfastsort-0.10-py3-none-any.whl (23.1 kB view details)

Uploaded Python 3

File details

Details for the file cythonfastsort-0.10.tar.gz.

File metadata

  • Download URL: cythonfastsort-0.10.tar.gz
  • Upload date:
  • Size: 23.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for cythonfastsort-0.10.tar.gz
Algorithm Hash digest
SHA256 593d0e814f72e82c11d0f01a85a0e517939052f8394f41b6c3a76e3203dffc0b
MD5 af2b3a68754275749998e24b336aaf1f
BLAKE2b-256 7b3d83b7df008b5874a6990c33e8626340e8e6f2365d4c3bf8169f55931de23f

See more details on using hashes here.

File details

Details for the file cythonfastsort-0.10-py3-none-any.whl.

File metadata

File hashes

Hashes for cythonfastsort-0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 7e4fafa75305797502b1c24deeedc0137970d435675f1a316a32d12db80ce8f6
MD5 fe0a0bf2cbd19fac44d554a31696cd89
BLAKE2b-256 47a1e3efe54cb2c53a1fd61694d5f2af6a0aea0bf2a916e42bf388d9c88aa46d

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