Skip to main content

Cartesian Product - 6x faster than itertools.product - 10x less memory

Project description

Cartesian Product - 6x faster than itertools.product - 10x less memory

pip install cythoncartesian

Tested against Windows / Python 3.11 / Anaconda

Cython (and a C/C++ compiler) must be installed

Generate the Cartesian product of input iterables.

Parameters:
-----------
*args : list of iterable
	Input iterables for which the Cartesian product will be computed.
outputdtype (optional) :
	dtype of output array
Returns:
--------
numpy.ndarray
	2D array containing the Cartesian product of the input iterables.

Notes:
------
This function efficiently computes the Cartesian product of the input iterables
using Cython implementation. It outperforms the equivalent functionality provided
by itertools.product, and returns a NumPy array (not a list of tuples like itertools.product).

Examples:
---------
	from cythoncartesian import cartesian_product

	# Mem usage 2 GB
	# Out[4]:
	# array([[0, 0, 0, ..., 0, 0, 0],
	#        [1, 0, 0, ..., 0, 0, 0],
	#        [2, 0, 0, ..., 0, 0, 0],
	#        ...,
	#        [5, 7, 7, ..., 7, 7, 7],
	#        [6, 7, 7, ..., 7, 7, 7],
	#        [7, 7, 7, ..., 7, 7, 7]], dtype=uint8)
	# %timeit dataresults=cartesian_product(*args2)
	# 2.65 s ± 163 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
	# dataresults.shape
	# Out[6]: (134217728, 9)
	

	# itertools.product
	# Mem usage 16 GB

	# import itertools
	# %timeit (list(itertools.product(*args2)))
	# 11.5 s ± 203 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)


	# --------------------------------------------------------------------------
	# Mem usage 1.2 GB
	# args = [[411, 231.33, 4342, 12341, 1.142, 1.33, 13],
	#         [34, 231.33, 4132, 1231],
	#          [14, 44, 23454.1, .1, 23, 1],
	#          [9, 12, 1, 3, 32, 23, 21, 31],
	#          [1114, 44, 23454.1, .1, 23, 1],
	#         ]+[list(range(6)),list(range(3)),list(range(3)),list(range(3))
	#     ,list(range(3))]+[list(range(6))]
	# %timeit dataresults=cartesian_product(*args)
	# 621 ms ± 46.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)


	# Mem usage 4 GB
	# import itertools
	# %timeit (list(itertools.product(*args)))
	# 2.13 s ± 26.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

Project details


Release history Release notifications | RSS feed

This version

0.10

Download files

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

Source Distribution

cythoncartesian-0.10.tar.gz (23.5 kB view details)

Uploaded Source

Built Distribution

cythoncartesian-0.10-py3-none-any.whl (23.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for cythoncartesian-0.10.tar.gz
Algorithm Hash digest
SHA256 6cf181dcad42059ca13b615762c9981a1ed3a4cc6eebe25263bd92e4402430d0
MD5 7f8c4bbf02db72688452cdb5c2f301fd
BLAKE2b-256 7842782d5e070bfda0f794119cea8a38acfd958159492c9e6e55581ab8474054

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cythoncartesian-0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 ccd52023cadcb2e6bfcaa189ba7a5275c9626acdcd6a372718178736c51f4e9a
MD5 38d43fa124e36dedbb389f201c3a1e75
BLAKE2b-256 558aed3cd7ea10f730ef12c399a06d16030efd5a2edfed248b1875f05ecb29cc

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