summaryrefslogtreecommitdiffstats
path: root/Doc/library
diff options
context:
space:
mode:
authorRaymond Hettinger <rhettinger@users.noreply.github.com>2022-10-15 21:05:28 (GMT)
committerGitHub <noreply@github.com>2022-10-15 21:05:28 (GMT)
commit3a639bbeace73d54f7e5431d3224c8c8223d81ae (patch)
treeef397c03939429b49ba91be1cc6c8aed44a79d00 /Doc/library
parent11c25a402d77bda507f8012ee2c14c95c835cf15 (diff)
downloadcpython-3a639bbeace73d54f7e5431d3224c8c8223d81ae.zip
cpython-3a639bbeace73d54f7e5431d3224c8c8223d81ae.tar.gz
cpython-3a639bbeace73d54f7e5431d3224c8c8223d81ae.tar.bz2
Improve speed. Reduce auxiliary memory to 16.6% of the main array. (GH-98294)
Diffstat (limited to 'Doc/library')
-rw-r--r--Doc/library/itertools.rst9
1 files changed, 5 insertions, 4 deletions
diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst
index 056b078..88e1e5a 100644
--- a/Doc/library/itertools.rst
+++ b/Doc/library/itertools.rst
@@ -822,12 +822,13 @@ which incur interpreter overhead.
def sieve(n):
"Primes less than n"
# sieve(30) --> 2 3 5 7 11 13 17 19 23 29
- data = bytearray([1]) * n
- data[:2] = 0, 0
+ data = bytearray((0, 1)) * (n // 2)
+ data[:3] = 0, 0, 0
limit = math.isqrt(n) + 1
for p in compress(range(limit), data):
- data[p*p : n : p] = bytearray(len(range(p*p, n, p)))
- return iter_index(data, 1)
+ data[p*p : n : p+p] = bytes(len(range(p*p, n, p+p)))
+ data[2] = 1
+ return iter_index(data, 1) if n > 2 else iter([])
def flatten(list_of_lists):
"Flatten one level of nesting"