Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

special-casing np.array(range(...)) #7233

Closed
anntzer opened this issue Feb 12, 2016 · 8 comments
Closed

special-casing np.array(range(...)) #7233

anntzer opened this issue Feb 12, 2016 · 8 comments
Labels
57 - Close? Issues which may be closable unless discussion continued

Comments

@anntzer
Copy link
Contributor

anntzer commented Feb 12, 2016

Compare (on Python3 -- for Python2, read "xrange" instead of "range"):

In [2]: %timeit np.array(range(1000000), np.int64)
10 loops, best of 3: 156 ms per loop

In [3]: %timeit np.arange(1000000, dtype=np.int64)
1000 loops, best of 3: 853 µs per loop

Note that while iterating over a range is not very fast, it is still much better than the array creation:

In [4]: from collections import deque

In [5]: %timeit deque(range(1000000), 1)
10 loops, best of 3: 25.5 ms per loop

On one hand, special cases are awful. On the other hand, the range builtin is probably important enough to deserve a special case. Or not? (Feel free to close this issue if that's not going to happen.)
(Real issue which prompted this suggestion: I was building sparse matrices using scipy.sparse.csc_matrix with some indices specified using range, and that construction step turned out to take a significant portion of the time because of the calls to np.array).

Cross-posted to the mailing list: https://mail.scipy.org/pipermail/numpy-discussion/2016-February/074960.html

@jakirkham
Copy link
Contributor

Seems like the sort of thing that should be put on the mailing list. Did you already put it on there? If so, could we please have a link?

@charris
Copy link
Member

charris commented Feb 14, 2016

Wouldn't it be better to fix sparse matrices or use arange in the first place?

@anntzer
Copy link
Contributor Author

anntzer commented Feb 14, 2016

Sure, this fix can go into scipy.sparse, but it's obviously more generally applicable than just for sparse matrices.

@charris
Copy link
Member

charris commented Feb 15, 2016

But that is what arange is for. What is the problem with arange?

@rgommers
Copy link
Member

rgommers commented Aug 1, 2018

I think I'd be for not special-casing this. We're also removing the special case for np.sum(generator). Close?

@rgommers rgommers added 57 - Close? Issues which may be closable unless discussion continued and removed 23 - Wish List labels Aug 1, 2018
@eric-wieser
Copy link
Member

np.sum(generator) was a special and inconsistent case of behavior, whereas this is just a suggested special case for optimization reasons. I don't think the reasoning is transferable here.

Having said that, I don't think this optimization is worthwhile - it seems like optimization efforts would be better invested elsewhere

@anntzer
Copy link
Contributor Author

anntzer commented Aug 4, 2018

I guess I may try my hand at implementing this at some point but feel free to close it for now...

@rgommers
Copy link
Member

rgommers commented Aug 7, 2018

Thanks @anntzer

@rgommers rgommers closed this as completed Aug 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
57 - Close? Issues which may be closable unless discussion continued
Projects
None yet
Development

No branches or pull requests

5 participants