Skip to content

Commit a2ae35d

Browse files
[3.11] Improve speed. Reduce auxiliary memory to 16.6% of the main array. (GH-98294) (GH-98303)
1 parent 79c0ade commit a2ae35d

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

Diff for: Doc/library/itertools.rst

+5-4
Original file line numberDiff line numberDiff line change
@@ -822,12 +822,13 @@ which incur interpreter overhead.
822822
def sieve(n):
823823
"Primes less than n"
824824
# sieve(30) --> 2 3 5 7 11 13 17 19 23 29
825-
data = bytearray([1]) * n
826-
data[:2] = 0, 0
825+
data = bytearray((0, 1)) * (n // 2)
826+
data[:3] = 0, 0, 0
827827
limit = math.isqrt(n) + 1
828828
for p in compress(range(limit), data):
829-
data[p*p : n : p] = bytearray(len(range(p*p, n, p)))
830-
return iter_index(data, 1)
829+
data[p*p : n : p+p] = bytes(len(range(p*p, n, p+p)))
830+
data[2] = 1
831+
return iter_index(data, 1) if n > 2 else iter([])
831832

832833
def flatten(list_of_lists):
833834
"Flatten one level of nesting"

0 commit comments

Comments
 (0)