mirror of
https://github.com/python/cpython
synced 2024-10-14 18:49:33 +00:00
Improve speed. Reduce auxiliary memory to 16.6% of the main array. (GH-98294)
This commit is contained in:
parent
11c25a402d
commit
3a639bbeac
|
@ -822,12 +822,13 @@ which incur interpreter overhead.
|
||||||
def sieve(n):
|
def sieve(n):
|
||||||
"Primes less than n"
|
"Primes less than n"
|
||||||
# sieve(30) --> 2 3 5 7 11 13 17 19 23 29
|
# sieve(30) --> 2 3 5 7 11 13 17 19 23 29
|
||||||
data = bytearray([1]) * n
|
data = bytearray((0, 1)) * (n // 2)
|
||||||
data[:2] = 0, 0
|
data[:3] = 0, 0, 0
|
||||||
limit = math.isqrt(n) + 1
|
limit = math.isqrt(n) + 1
|
||||||
for p in compress(range(limit), data):
|
for p in compress(range(limit), data):
|
||||||
data[p*p : n : p] = bytearray(len(range(p*p, n, p)))
|
data[p*p : n : p+p] = bytes(len(range(p*p, n, p+p)))
|
||||||
return iter_index(data, 1)
|
data[2] = 1
|
||||||
|
return iter_index(data, 1) if n > 2 else iter([])
|
||||||
|
|
||||||
def flatten(list_of_lists):
|
def flatten(list_of_lists):
|
||||||
"Flatten one level of nesting"
|
"Flatten one level of nesting"
|
||||||
|
|
Loading…
Reference in a new issue