[New-bugs-announce] [issue47221] Bug or bad performance

Cezary Wagner report at bugs.python.org
Mon Apr 4 18:00:20 EDT 2022


New submission from Cezary Wagner <cezary.wagner at gmail.com>:

I am experienced programmer 10y+ - that is very very strange performance problem when I play Python timeit with my son :)

three way operator a <= x <= b is slower than a <= x and x <= b.

It looks like wrong implementation since it is impossible that two separate check is faster that one check (with two low level check in C).




import timeit

REPEATS = 100


def test1():
    selected = []
    for i in range(REPEATS):
        if i >= 25 and i <= 75:
            selected.append(i)
    return selected


def test2():
    selected = []
    for i in range(REPEATS):
        if 25 <= i <= 75:
            selected.append(i)
    return selected


print(timeit.timeit(test1))
print(timeit.timeit(test2))


Result is on Windows 10.
4.428947699998389
4.9062477999978

----------
components: Interpreter Core
messages: 416699
nosy: Cezary.Wagner
priority: normal
severity: normal
status: open
title: Bug or bad performance
type: performance
versions: Python 3.10

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue47221>
_______________________________________


More information about the New-bugs-announce mailing list