Skip to main content

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)

Metadata

Release files for cythoncartesian 0.10

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

Source distribution (sdist)

Source distribution for cythoncartesian 0.10
File Size Uploaded Provenance Actions
cythoncartesian-0.10.tar.gz 23.5 kB Without provenance Details for cythoncartesian-0.10.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for cythoncartesian 0.10
File Interpreter ABI Platform Actions
cythoncartesian-0.10-py3-none-any.whl Python 3 none any Details for cythoncartesian-0.10-py3-none-any.whl

Total release size: 47.4 kB

Release files / cythoncartesian-0.10.tar.gz

Download URL cythoncartesian-0.10.tar.gz
Size 23.5 kB
Tags Source
SHA-256 checksum
How to use checksums
6cf181dcad42059ca13b615762c9981a1ed3a4cc6eebe25263bd92e4402430d0
BLAKE2b-256 checksum
How to use checksums
7842782d5e070bfda0f794119cea8a38acfd958159492c9e6e55581ab8474054
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.5

Release files / cythoncartesian-0.10-py3-none-any.whl

Download URL cythoncartesian-0.10-py3-none-any.whl
Size 23.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ccd52023cadcb2e6bfcaa189ba7a5275c9626acdcd6a372718178736c51f4e9a
BLAKE2b-256 checksum
How to use checksums
558aed3cd7ea10f730ef12c399a06d16030efd5a2edfed248b1875f05ecb29cc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.5

Security for cythoncartesian 0.10

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.10 This release

2 release files No provenance

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