pyscroll icon indicating copy to clipboard operation
pyscroll copied to clipboard

Performance improvement dump

Open joereynolds opened this issue 1 year ago • 1 comments

Apologies if this is the wrong place (feel free to close if so) but I thought I'd gather some cheap wins on the performance-side of pyscroll.

  • [ ] - Convert list() to [] (more below)
    • I haven't looked everywhere but orthographic.py has a load of these which are low hanging fruit
  • [ ] - Convert fors to comprehensions (more below)
    • Again, I haven't looked elsewhere but orthographic.py has a few instance that we could speed up
  • [ ] - Convert dict() to {}
    • Same deal as the lists. {} is faster. There's only two occurrences of dict() in the repo so this isn't really worth doing but worth keeping in mind for future
  • [ ] - Experiment with fblits
    • This would be a breaking change as it's pygame-ce only but apparently has some performance gains. I tried a quickl replace of blits -> fblits and sadly saw no improvements on my end but maybe with some reworking we could get some good gains. Ref: https://pyga.me/docs/ref/surface.html#pygame.Surface.fblits

Convert list() to []

[] is faster than list() A million runs with each gives the following

[] took  0.06097865104675293
list() took  0.10919380187988281

The script is

import time

limit = 1000000

start = time.time()
for i in range(limit):
    a = []
end = time.time()
duration = end - start
print('[] took ', duration)

start = time.time()
for i in range(limit):
    a = list()
end = time.time()
duration = end - start
print('list() took ', duration)

Convert fors to list comprehensions

Same story. List comprehensions run significantly quicker than fors and there's a few places where we can optimise.

A thousand runs of populating 10000 numbers gave me

comprehension took  0.2832183837890625
for took  0.6464605331420898

The script is

import time

limit = 1000

start = time.time()
for i in range(limit):
    a = [x +1 for x in range(10000)]
end = time.time()
duration = end - start
print('comprehension took ', duration)

start = time.time()
for i in range(limit):
    a = []
    app = a.append
    for x in range(10000):
        app(x+1)
end = time.time()
duration = end - start
print('for took ', duration)

If there's a reason these are left as they are then I'm happy to leave it but otherwise I might start tackling these soon. Thanks and feel free to add to my list!

joereynolds avatar Aug 07 '24 13:08 joereynolds

@joereynolds dict() and list() are done, both added to my PR

JaskRendix avatar Aug 26 '24 08:08 JaskRendix