-
Notifications
You must be signed in to change notification settings - Fork 30
Open
Description
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.pyhas a load of these which are low hanging fruit
- I haven't looked everywhere but
- - Convert
fors to comprehensions (more below)- Again, I haven't looked elsewhere but
orthographic.pyhas a few instance that we could speed up
- Again, I haven't looked elsewhere but
- - Convert
dict()to{}- Same deal as the lists.
{}is faster. There's only two occurrences ofdict()in the repo so this isn't really worth doing but worth keeping in mind for future
- Same deal as the lists.
- - 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->fblitsand 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
- This would be a breaking change as it's pygame-ce only but apparently has some performance gains. I tried a quickl replace of
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!
Metadata
Metadata
Assignees
Labels
No labels