forked from brennerm/PyTricks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
boolisslow.py
42 lines (30 loc) · 879 Bytes
/
boolisslow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""
True and False are keywords in python3, but not in Python2. In Python2
the value of True is 1, and the value of False is 0. True and False can be
regarded as vars and thus they are slow in while loop.
By the way, True and False can be changed in Python2.
Note: This leads to a performance improvement only in Python2.
"""
from timeit import timeit
def test_true():
count = 1000
while True: # here is True
if count < 0:
break
count -= 1
def test_1():
count = 1000
while 1: # here is 1
if count < 0:
break
count -= 1
# on my macbook pro 15
# test_true is about 59.3047289848 seconds
# test_1 is about 39.0966179371 seconds
print(timeit(test_true, number=1000000))
print(timeit(test_1, number=1000000))
# True and False can be changed in python2
True = 0
False = 100
print(True)
print(False)