The overall body reads like an
if
tree, yet all scenarios are mutually exclusive. You can probably squeak a little better performance out by changing the second and laterif
s toelif
s. Make sure they're ordered in terms of descending frequency. If the number of possibilities will grow larger, consider other approaches that don't require a series ofif
tests. (At some point the overhead of testing each alternative will likely be more expensive than that of using alternate lookups such as draw method on various tile classes.)The distance check loop in a
res
tile seems flawed. It executes a for loop, but only saves the result from the last element ofothertiles
. And for each element, it checks ifothertiles
is empty (aside: you should spell thatif othertiles:
rather than creating and comparing against an empty list every time). If you want the first result, you can fix both of these by usingbreak
and afor/else
loop:for x in othertiles: # distance calculation break else: t.haspower = 0
(Alternately you could just set
t.haspower = 0
—orFalse
—before the for loop, and let the for loop set it to1
—orTrue
—if applicable.)The distance calculation is computationally heavy. Like my comment on your previous post previous post, this is typically more relevant to C level code, but it's still good to remember that
sqrt
takes a while to calculate, so you can look for opportunities to avoid calling it. Consider that you compareDist(...)
to a constant2500
. If instead of callingsqrt
you just returned the square of the distance, you could compare to6250000
and have the same effect.Calling the distance function, you tear
t.pos
andx.pos
apart and piece it back together for a function that just uses their first two elements. You might save some effort, readability, and possibly gain a hint of performance by calling your distance function with justDist(t.pos, x.pos)
.
The overall body reads like an
if
tree, yet all scenarios are mutually exclusive. You can probably squeak a little better performance out by changing the second and laterif
s toelif
s. Make sure they're ordered in terms of descending frequency. If the number of possibilities will grow larger, consider other approaches that don't require a series ofif
tests. (At some point the overhead of testing each alternative will likely be more expensive than that of using alternate lookups such as draw method on various tile classes.)The distance check loop in a
res
tile seems flawed. It executes a for loop, but only saves the result from the last element ofothertiles
. And for each element, it checks ifothertiles
is empty (aside: you should spell thatif othertiles:
rather than creating and comparing against an empty list every time). If you want the first result, you can fix both of these by usingbreak
and afor/else
loop:for x in othertiles: # distance calculation break else: t.haspower = 0
(Alternately you could just set
t.haspower = 0
—orFalse
—before the for loop, and let the for loop set it to1
—orTrue
—if applicable.)The distance calculation is computationally heavy. Like my comment on your previous post, this is typically more relevant to C level code, but it's still good to remember that
sqrt
takes a while to calculate, so you can look for opportunities to avoid calling it. Consider that you compareDist(...)
to a constant2500
. If instead of callingsqrt
you just returned the square of the distance, you could compare to6250000
and have the same effect.Calling the distance function, you tear
t.pos
andx.pos
apart and piece it back together for a function that just uses their first two elements. You might save some effort, readability, and possibly gain a hint of performance by calling your distance function with justDist(t.pos, x.pos)
.
The overall body reads like an
if
tree, yet all scenarios are mutually exclusive. You can probably squeak a little better performance out by changing the second and laterif
s toelif
s. Make sure they're ordered in terms of descending frequency. If the number of possibilities will grow larger, consider other approaches that don't require a series ofif
tests. (At some point the overhead of testing each alternative will likely be more expensive than that of using alternate lookups such as draw method on various tile classes.)The distance check loop in a
res
tile seems flawed. It executes a for loop, but only saves the result from the last element ofothertiles
. And for each element, it checks ifothertiles
is empty (aside: you should spell thatif othertiles:
rather than creating and comparing against an empty list every time). If you want the first result, you can fix both of these by usingbreak
and afor/else
loop:for x in othertiles: # distance calculation break else: t.haspower = 0
(Alternately you could just set
t.haspower = 0
—orFalse
—before the for loop, and let the for loop set it to1
—orTrue
—if applicable.)The distance calculation is computationally heavy. Like my comment on your previous post, this is typically more relevant to C level code, but it's still good to remember that
sqrt
takes a while to calculate, so you can look for opportunities to avoid calling it. Consider that you compareDist(...)
to a constant2500
. If instead of callingsqrt
you just returned the square of the distance, you could compare to6250000
and have the same effect.Calling the distance function, you tear
t.pos
andx.pos
apart and piece it back together for a function that just uses their first two elements. You might save some effort, readability, and possibly gain a hint of performance by calling your distance function with justDist(t.pos, x.pos)
.
Humans are bad at figuring out where the slow spots are. You will need to ask the computer by profiling your code. As rolfl said, you're likely better off saving this for when you have more of your code in place and it does enough work that it's actually slowing down.
It's quite possible most of your slowdown related to extra tiles comes from othertiles=[x for x in tiles if x.spr==power1 or x.spr==power2]
; perhaps you should update othertiles
incrementally as the board changes instead of recalculating it every pygame.event
. Other than that, I'm going to concentrate on the inner for t in tiles
loop. There are a couple things in there that stand out to me. They may or may not make a real difference to your code's performance.
The overall body reads like an
if
tree, yet all scenarios are mutually exclusive. You can probably squeak a little better performance out by changing the second and laterif
s toelif
s. Make sure they're ordered in terms of descending frequency. If the number of possibilities will grow larger, consider other approaches that don't require a series ofif
tests. (At some point the overhead of testing each alternative will likely be more expensive than that of using alternate lookups such as draw method on various tile classes.)The distance check loop in a
res
tile seems flawed. It executes a for loop, but only saves the result from the last element ofothertiles
. And for each element, it checks ifothertiles
is empty (aside: you should spell thatif othertiles:
rather than creating and comparing against an empty list every time). If you want the first result, you can fix both of these by usingbreak
and afor/else
loop:for x in othertiles: # distance calculation break else: t.haspower = 0
(Alternately you could just set
t.haspower = 0
—orFalse
—before the for loop, and let the for loop set it to1
—orTrue
—if applicable.)The distance calculation is computationally heavy. Like my comment on your previous post, this is typically more relevant to C level code, but it's still good to remember that
sqrt
takes a while to calculate, so you can look for opportunities to avoid calling it. Consider that you compareDist(...)
to a constant2500
. If instead of callingsqrt
you just returned the square of the distance, you could compare to6250000
and have the same effect.Calling the distance function, you tear
t.pos
andx.pos
apart and piece it back together for a function that just uses their first two elements. You might save some effort, readability, and possibly gain a hint of performance by calling your distance function with justDist(t.pos, x.pos)
.
Two more notes:
You probably don't want just the first or last distance to a power tile. Instead you likely want the closest. This might be easier to handle with an approach like
distance = min(Dist(t.pos, x.pos) for x in othertiles)) if othertiles else 2501
or even better:t.haspower = False if othertiles: t.haspower = min(Dist(t.pos, x.pos) for x in othertiles)) < 2500 # or distance_squared and compare to 6250000 per third bullet
It's unusual in python to name your functions with initial capital letters. When I read the line
distance=Dist((t.pos[0],t.pos[1]),(x.pos[0],x.pos[1]))
I assumed it was instantiating a class (which would probably be even heavier than calculating a square root). I would suggest renaming this use all lowercase letters.