Fast implementation of unique elements in an array using parallel sorting
Project description
Fast implementation of unique elements in an array using parallel sorting
pip install cythonunique
Tested against Windows / Python 3.11 / Anaconda
Cython (and a C/C++ compiler) must be installed to use the optimized Cython implementation.
This module provides a fast implementation of unique elements in an array using parallel sorting.
Functions:
- fast_unique(arr): Returns an array containing unique elements from the input array.
import timeit
import numpy as np
from cythonunique import fast_unique
def generate_random_arrays(shape, dtype='float64', low=0, high=1):
return np.random.uniform(low, high, size=shape).astype(dtype)
size = 10000000
low = 0
high = 254
arras = [
(size, 'float32', low, high),
(size, 'float64', low, high),
(size, np.uint8, low, high),
(size, np.int8, low, high),
(size, np.int16, low, high),
(size, np.int32, low, high),
(size, np.int64, low, high),
(size, np.uint16, low, high),
(size, np.uint32, low, high),
(size, np.uint64, low, high),
]
reps = 5
for a in arras:
arr = generate_random_arrays(*a)
s = """u=fast_unique(arr)"""
t1 = timeit.timeit(s, globals=globals(), number=reps) / reps
print(t1)
s = """u=np.unique(arr)"""
t2 = timeit.timeit(s, globals=globals(), number=reps) / reps
print(t2)
u = fast_unique(arr)
q = np.unique(arr)
print(np.all(u == q))
print('-------------------------')
# 0.10624040000111563
# 0.14222411999944598
# True
# -------------------------
# 0.10455849999998464
# 0.1573818399992888
# True
# -------------------------
# 0.03686119999911171
# 0.0845491200001561
# True
# -------------------------
# 0.03726967999973567
# 0.08551521999906982
# True
# -------------------------
# 0.038274600000295325
# 0.10370338000066112
# True
# -------------------------
# 0.046226339999702756
# 0.10226724000094692
# True
# -------------------------
# 0.06615012000111165
# 0.1257952400002978
# True
# -------------------------
# 0.04093761999974958
# 0.10167090000031749
# True
# -------------------------
# 0.04656872000050498
# 0.10330443999991985
# True
# -------------------------
# 0.06586845999991056
# 0.13191485999996075
# True
# -------------------------
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
cythonunique-0.10.tar.gz
(21.9 kB
view hashes)
Built Distribution
Close
Hashes for cythonunique-0.10-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 478b7225cc5fe02ee911c65d501a4534f4550a2a6a6fd4884970c8687d990d5d |
|
MD5 | 4a692c0f723b8023126f374d64e99bd9 |
|
BLAKE2b-256 | 3cd0a0669afd8d12af20fbb5e134266f19a4946f816fe5a0f9d7bfdbe30fe10a |