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
Support heapq on typed arrays? #77774
Comments
It'd be really great if we could have support for using the
Right now this code throws a TypeError:
I suppose I could use From what I can tell the problem lies in the C implementation, since removing the _heapq imports at the end of the heapq module (in 3.6) makes it work. |
I don't think we should go down this path. The efficiency of the C implementation depends on it being tightly coupled to lists. This tool is used in the schedulers of various async tools (such as Tornando), used for merge(), nsmallest(), and nlargest() all of which depend on this foundational tool being very fast. Also, I question whether it makes sense at all to be heapifying numpy arrays using standard library tooling. It numpy arrays actually needed this and needed for it to be efficient, it would need to be implemented natively in numpy. |
I was referring to the C arrays in the Python standard library: https://docs.python.org/3/library/array.html |
However I do see your point about the speed. |
Workaround: alist = list(a)
heapq.heapify(alist)
a[:] = alist And it should be not much slower than using heapq.heapify() directly if it could support general sequences. Using it with array.array would add significant overhead due to boxing. |
As noted by Serhiy, the interaction with the Array type would incur significant overhead. Your fastest approach will be to follow his suggest to first convert to a list and then perform heap manipulations. Marking this as closed. Thank you for the suggestion. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: