1

I've read this question: Why is if True slower than if 1?, but i'm using Python3 now. I am writing leetcode 204 and someone said that using one is faster than using two.

My code:

import timeit

count = 1000


def countPrimes(n=100000):
    if n < 3:
        return 0
    primes = [True] * n
    primes[0], primes[1] = False, False
    for i in range(2, int(n ** 0.5) + 1):
        if primes[i]:
            for j in range(i * i, n, i):
                primes[j] = False
    return sum(primes)


def countPrimes2(n=100000):
    if n < 3:
        return 0
    primes = [1] * n
    primes[0], primes[1] = 0, 0
    for i in range(2, int(n ** 0.5) + 1):
        if primes[i]:
            for j in range(i * i, n, i):
                primes[j] = 0
    return sum(primes)


print('use False,True                 ', timeit.timeit(countPrimes, number=count))
print('use 0,1                        ', timeit.timeit(countPrimes2, number=count))

The result is:

-> % python3 test.py 
use False,True                  10.634566191001795
use 0,1                         9.187052419991232

Can anyone tell me why?

PoByBolek
  • 3,775
  • 3
  • 21
  • 22
peter zhang
  • 1,247
  • 1
  • 10
  • 19
  • 8
    maybe because `sum(primes)` has to convert all `True/False` to `0/1` ? – furas Sep 08 '19 at 02:42
  • Agree with Furas. It may depends on your machine etc. but solely the conversion will have created more than 1 ms of the time difference. For the rest I recommend debugging the specific functions. – Kay Wittig Sep 08 '19 at 18:11
  • I encountered the same behavior on pypy without using sum – qwr Feb 19 '23 at 01:23

1 Answers1

0

ok, as furas said, it's the sum cost much time:

import timeit

count = 1000

def countPrimes(n=100000):
    primes = [True] * n
    return sum(primes)

def countPrimes2(n=100000):
    primes = [1] * n
    return sum(primes)

print('use False,True                 ', timeit.timeit(countPrimes, number=count))
print('use 0,1                        ', timeit.timeit(countPrimes2, number=count))

result is:

-> % python3 test.py
use False,True                  2.4546820300165564
use 0,1                         1.0242620470235124

without sum:

import timeit

count = 1000

def countPrimes(n=100000):
    if n < 3:
        return 0
    primes = [True] * n
    primes[0], primes[1] = False, False
    for i in range(2, int(n ** 0.5) + 1):
        if primes[i]:
            for j in range(i * i, n, i):
                primes[j] = False

def countPrimes2(n=100000):
    if n < 3:
        return 0
    primes = [1] * n
    primes[0], primes[1] = 0, 0
    for i in range(2, int(n ** 0.5) + 1):
        if primes[i]:
            for j in range(i * i, n, i):
                primes[j] = 0

print('use False,True                 ', timeit.timeit(countPrimes, number=count))
print('use 0,1                        ', timeit.timeit(countPrimes2, number=count))

result is:

-> % python3 test.py
use False,True                  8.47771276300773
use 0,1                         8.44074950099457

time is almost the same

peter zhang
  • 1,247
  • 1
  • 10
  • 19