pygame
pygame copied to clipboard
Rect functions improvements
Info
One of #3337. This PR speeds up some rect functions that recieve a list of rects as an argument. Improvements consist in the use of METH_O, as those functions just take 1 argument and PySequence_Fast, to replace the normal slower Sequence we are using now . Improvements vary and can go from 25% up to 105% depending on the situation. THIS PR IS A DRAFT AND WILL ALWAYS BE. ONCE #3230 GETS MERGED I'LL OPEN A NEW PR THAT WILL IMPLEMENT THESE CHANGES IN THE NEW FILES/DIRS.
Some performance tests (not precise. assume the rect used is always the same for each rect):
OLD:

CUMULATIVE TIME: COLLIDELIST | 0.0657496 COLLIDELISTALL | 0.0851887 UNIONALL | 0.0667247 UNIONALLIP | 0.0564686001
MEAN: COLLIDELIST | 0.000131499 COLLIDELISTALL | 0.000170377 UNIONALL | 0.000133449 UNIONALLIP | 0.000112937
NEW

CUMULATIVE TIME: COLLIDELIST | 0.0327105 COLLIDELISTALL | 0.0491473 UNIONALL | 0.0377327 UNIONALLIP | 0.0290111001
MEAN: COLLIDELIST | 6.5421e-05 COLLIDELISTALL | 9.8295e-05 UNIONALL | 7.5465e-05 UNIONALLIP | 5.8022e-05
Update on performance. After careful and more general testing i can say that (Best Case: all rects are normalized) the mean improvement across all tests is 41.6%. The smallest performance improvment is the one of collidelist() which stands at approx. 20%. All the others are around 45-50%. (Unlikely but worse case: mixed normalized and not normalized) The mean improvement goes down to 32,57 %, with the wost improvement still held by collidelist() at 8% improv.
Some newer performance tests: OLD
CUMULATIVE TIME:
COLLIDELIST | 0.0465923
COLLIDELISTALL | 0.1035984
UNIONALL | 0.0792499
UNIONALLIP | 0.0672198
COLLIDEOBJECTS | 0.1883542
COLLIDEOBJECTS_ALL | 0.4826421
MEAN:
COLLIDELIST | 9.3185e-05
COLLIDELISTALL | 0.000207197
UNIONALL | 0.0001585
UNIONALLIP | 0.00013444
COLLIDEOBJECTS | 0.000376708
COLLIDEOBJECTS_ALL | 0.000965284
NEW
CUMULATIVE TIME:
COLLIDELIST | 0.0201688
COLLIDELISTALL | 0.058704
UNIONALL | 0.0394498
UNIONALLIP | 0.030145
COLLIDEOBJECTS | 0.1597061
COLLIDEOBJECTS_ALL | 0.4349253
MEAN:
COLLIDELIST | 4.0338e-05
COLLIDELISTALL | 0.000117408
UNIONALL | 7.89e-05
UNIONALLIP | 6.029e-05
COLLIDEOBJECTS | 0.000319412
COLLIDEOBJECTS_ALL | 0.000869851
Improvements
COLLIDELIST | 131% better
COLLIDELISTALL | 76.47% better
UNIONALL | 100.88% better
UNIONALLIP | 122.9% better
COLLIDEOBJECTS | 17.9%better
COLLIDEOBJECTS_ALL | 10.97% better
NOTE. This does not mean that functions will be 100% faster across the board. These results refer to a list of rects of 10 elements, all being Rect instances. Longer sequences will possibly yield smaller benefits. Like for example COLLIDELISTALL with a 20 rects list will be 64% better than the old implementation,.