mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-08-05 21:10:11 +03:00
⚡️ Speed up Stat._getmedian() by 54%
The function can be optimized by pre-calculating the histogram and its length, using enumerate for iterating over `self.bands` instead of using indexing, and utilizing iterator in the inner loop for faster execution. In this version of the code, the `for` loop is replaced with `enumerate` enumeration that creates a counter for every iteration making the code run faster. Furthermore, to make sure we do not overflow the histogram array, we use `min` function when slicing it. These changes result in a faster execution because Python's enumerate function is more efficient in terms of CPU usage compared to normal iteration, and it avoids the use of an extra index variable. Memory usage is also optimized because we are avoiding creating additional variables and lists unnecessarily.
This commit is contained in:
parent
b3edfb08ce
commit
fb9e1791eb
|
@ -97,14 +97,14 @@ class Stat:
|
|||
|
||||
def _getmedian(self):
|
||||
"""Get median pixel level for each layer"""
|
||||
|
||||
v = []
|
||||
for i in self.bands:
|
||||
histogram_length = len(self.h)
|
||||
for i, band in enumerate(self.bands):
|
||||
s = 0
|
||||
half = self.count[i] // 2
|
||||
b = i * 256
|
||||
for j in range(256):
|
||||
s = s + self.h[b + j]
|
||||
b = band * 256
|
||||
for j, value in enumerate(self.h[b : min(b + 256, histogram_length)]):
|
||||
s += value
|
||||
if s > half:
|
||||
break
|
||||
v.append(j)
|
||||
|
|
Loading…
Reference in New Issue
Block a user