Python Tricks #7 - True and False are slow

in programming •  5 years ago 
"""
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.
"""
import sys
from timeit import timeit


def test_true():
    count = 100
    while True:  # here is True
        if count < 0:
            break
        count -= 1


def test_1():
    count = 100
    while 1:  # here is 1
        if count < 0:
            break
        count -= 1


# test_true is about 5.01579904556 seconds
# test_1 is about 3.70646500587 seconds
print(timeit(test_true, number=1000000))
print(timeit(test_1, number=1000000))
Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Interesting.

I've just tried it out on Python 3.7.3. Most of the time test_1 is coming in at around 3.9xx and test_true is around 4.0xx with about a tenth of a second difference. A few times though, the timings flipped round and test_true was marginally faster.

When it's not almost 11pm I might have a fiddle and see if I can run them side by side and produce better results.

Thanks for the comment! :)

Posted using Partiko Android