Similar to why use True is slower than use 1 in Python3 but I'm using pypy3 and not using the sum function.
def sieve_num(n):
nums = [0] * n
for i in range(2, n):
if i * i >= n: break
if nums[i] == 0:
for j in range(i*i, n, i):
nums[j] = 1
return [i for i in range(2, n) if nums[i] == 0]
def sieve_bool(n):
nums = [False] * n
for i in range(2, n):
if i * i >= n: break
if nums[i] == False:
for j in range(i*i, n, i):
nums[j] = True
return [i for i in range(2, n) if nums[i] == False]
sieve_num(10**8)
takes 2.55 s, but sieve_bool(10**8)
takes 4.45 s, which is a noticeable difference.
My suspicion was that [0]*n
is somehow smaller than [False]*n
and fits into cache better, but sys.getsizeof
and vmprof line profiling are unsupported for PyPy. The only info I could get is that <listcomp>
for sieve_num
took 116 ms (19% of total execution time) while <listcomp>
for sieve_bool
tool 450 ms (40% of total execution time).
Using PyPy 7.3.1 implementing Python 3.6.9 on Intel i7-7700HQ with 24 GB RAM on Ubuntu 20.04. With Python 3.8.10 sieve_bool
is only slightly slower.