[issue47221] Bug or bad performance

2022-04-04 Thread Dennis Sweeney


Dennis Sweeney  added the comment:

I believe this is a duplicate of this issue: https://bugs.python.org/issue45542

--
nosy: +Dennis Sweeney

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue47221] Bug or bad performance

2022-04-04 Thread Cezary Wagner


Cezary Wagner  added the comment:

Some more experiments:

import dis
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


def test3():
return [x for x in range(REPEATS) if x >= 25 and x <= 75]


def test4():
return [x for x in range(REPEATS) if 25 <= x <= 75]


def test(f):
print(dis.dis(f.__code__.co_code))
print(timeit.timeit(f))


test(test1)
test(test2)
test(test3)
test(test4)

Result:

  0 BUILD_LIST   0
  2 STORE_FAST   0 (0)
  4 LOAD_GLOBAL  0 (0)
  6 LOAD_GLOBAL  1 (1)
  8 CALL_FUNCTION1
 10 GET_ITER
>>   12 FOR_ITER15 (to 44)
 14 STORE_FAST   1 (1)
 16 LOAD_FAST1 (1)
 18 LOAD_CONST   1 (1)
 20 COMPARE_OP   5 (>=)
 22 POP_JUMP_IF_FALSE   21 (to 42)
 24 LOAD_FAST1 (1)
 26 LOAD_CONST   2 (2)
 28 COMPARE_OP   1 (<=)
 30 POP_JUMP_IF_FALSE   21 (to 42)
 32 LOAD_FAST0 (0)
 34 LOAD_METHOD  2 (2)
 36 LOAD_FAST1 (1)
 38 CALL_METHOD  1
 40 POP_TOP
>>   42 JUMP_ABSOLUTE6 (to 12)
>>   44 LOAD_FAST0 (0)
 46 RETURN_VALUE
None
4.56567799025
  0 BUILD_LIST   0
  2 STORE_FAST   0 (0)
  4 LOAD_GLOBAL  0 (0)
  6 LOAD_GLOBAL  1 (1)
  8 CALL_FUNCTION1
 10 GET_ITER
>>   12 FOR_ITER19 (to 52)
 14 STORE_FAST   1 (1)
 16 LOAD_CONST   1 (1)
 18 LOAD_FAST1 (1)
 20 DUP_TOP
 22 ROT_THREE
 24 COMPARE_OP   1 (<=)
 26 POP_JUMP_IF_FALSE   18 (to 36)
 28 LOAD_CONST   2 (2)
 30 COMPARE_OP   1 (<=)
 32 POP_JUMP_IF_FALSE   25 (to 50)
 34 JUMP_FORWARD 2 (to 40)
>>   36 POP_TOP
 38 JUMP_ABSOLUTE6 (to 12)
>>   40 LOAD_FAST0 (0)
 42 LOAD_METHOD  2 (2)
 44 LOAD_FAST1 (1)
 46 CALL_METHOD  1
 48 POP_TOP
>>   50 JUMP_ABSOLUTE6 (to 12)
>>   52 LOAD_FAST0 (0)
 54 RETURN_VALUE
None
5.639823407506
  0 LOAD_CONST   1 (1)
  2 LOAD_CONST   2 (2)
  4 MAKE_FUNCTION0
  6 LOAD_GLOBAL  0 (0)
  8 LOAD_GLOBAL  1 (1)
 10 CALL_FUNCTION1
 12 GET_ITER
 14 CALL_FUNCTION1
 16 RETURN_VALUE
None
3.879290759425
  0 LOAD_CONST   1 (1)
  2 LOAD_CONST   2 (2)
  4 MAKE_FUNCTION0
  6 LOAD_GLOBAL  0 (0)
  8 LOAD_GLOBAL  1 (1)
 10 CALL_FUNCTION1
 12 GET_ITER
 14 CALL_FUNCTION1
 16 RETURN_VALUE
None
3.859126637943

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue47221] Bug or bad performance

2022-04-04 Thread Cezary Wagner


New submission from Cezary Wagner :

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.42894768389
4.906247778

--
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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com