Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance improvement dump #63

Open
4 tasks
joereynolds opened this issue Aug 7, 2024 · 1 comment
Open
4 tasks

Performance improvement dump #63

joereynolds opened this issue Aug 7, 2024 · 1 comment

Comments

@joereynolds
Copy link

joereynolds commented Aug 7, 2024

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!

@JaskRendix
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants