Skip to content

Conversation

@crusaderky
Copy link
Collaborator

@crusaderky crusaderky commented Jul 1, 2022

math.prod requires Python 3.8+, so historically it wasn't used in the codebase.
It's 20~40x faster than numpy.prod on pure python lists and tuples, and it doesn't need to convert iterators to lists, resulting in cleaner code.

>>> a = (1,2,3)
>>> %timeit numpy.prod(a)
3.27 µs ± 51.9 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
>>> %timeit math.prod(a)
92.5 ns ± 0.38 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)
>>> %timeit numpy.prod(list(range(1, 4)))
3.42 µs ± 19.2 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
>>> %timeit math.prod(range(1, 4))
159 ns ± 0.677 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)

Also, numpy.prod returns a numpy.int64, which can cause problems down the line.

@github-actions github-actions bot added the array label Jul 1, 2022
@crusaderky crusaderky self-assigned this Jul 1, 2022
@crusaderky crusaderky marked this pull request as ready for review July 1, 2022 12:55
Copy link
Member

@jsignell jsignell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great! Really nice cleanup :)

from operator import add, sub
from threading import Lock

from numpy import nancumprod, nancumsum
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for doing this cleanup!

@jsignell
Copy link
Member

jsignell commented Jul 6, 2022

rerun tests

@jsignell jsignell merged commit 785c845 into dask:main Jul 6, 2022
@jsignell
Copy link
Member

jsignell commented Jul 6, 2022

Thanks for this @crusaderky!! This feels like a pure improvement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants