Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Python list is not an array #22

Closed
alegrigoriev opened this issue Mar 27, 2021 · 4 comments
Closed

Python list is not an array #22

alegrigoriev opened this issue Mar 27, 2021 · 4 comments

Comments

@alegrigoriev
Copy link

alegrigoriev commented Mar 27, 2021

Python's list buitin doesn't do random access, it's not an array. bytearray builtin is a real array.
Yet, somehow the bytearray gains not too much.
On my setup, the original version does 20 passes in 10 seconds, an with the bytearray it only does 42 passes.
Also, every step of the sieve can actually start from factor*factor, because every N*factor for N<factor has already been eliminated. This doesn't give much gain, though.

@BrunoDSL
Copy link

BrunoDSL commented Apr 5, 2021

If you want something closer to an actual array, the deque builtin from the collections module is the one to use.

@sueskind
Copy link

sueskind commented Apr 5, 2021

Actually, Python lists do have an amortized item access complexity of O(1). Internally it is represented as an array. However, pop intermediate and insert are O(n).

@alegrigoriev
Copy link
Author

Actually, Python lists do have an amortized item access complexity of O(1). Internally it is represented as an array. However, pop intermediate and insert are O(n).

Thank you. I actually looked at the python sources after I posted it and, sure, the list() is implemented as an array.

@aberent
Copy link

aberent commented Apr 7, 2021

Python does also have a true array type in the standard library. See https://docs.python.org/3/library/array.html for details. I use this in my version of the prime sieve (https://github.com/davepl/Primes/pull/36)

@rbergen rbergen closed this as completed Jun 4, 2021
@PlummersSoftwareLLC PlummersSoftwareLLC locked and limited conversation to collaborators Jun 4, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants