Skip to content

Commit 11a539c

Browse files
author
BoboTiG
committed
MSSLinux: use of memoization => huge time/operations gains
1 parent 1b2bc39 commit 11a539c

File tree

1 file changed

+7
-16
lines changed

1 file changed

+7
-16
lines changed

mss.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -530,23 +530,14 @@ def get_pixels(self, monitor):
530530
if image is None:
531531
raise ValueError('MSSLinux: XGetImage() failed.')
532532

533-
# TODO: how to optimize this part? pixels[offset:offset+3] is too long.
534-
'''
535-
pixels = [b'0'] * (3 * width * height)
536-
for x in range(width):
537-
for y in range(height):
538-
pixel = self.XGetPixel(image, x, y)
539-
blue = pixel & 255
540-
green = (pixel & 65280) >> 8
541-
red = (pixel & 16711680) >> 16
542-
offset = (x + width * y) * 3
543-
pixels[offset:offset+3] = b(red), b(green), b(blue)
544-
#'''
545-
546-
# This code is a little bit better (19% faster)
533+
resultats={}
547534
def pix(pixel):
548-
''' Apply shifts to a pixel to get the RGB values. '''
549-
return b((pixel & 16711680) >> 16) + b((pixel & 65280) >> 8) + b(pixel & 255)
535+
''' Apply shifts to a pixel to get the RGB values.
536+
This method uses of memoization, reducing time by a factor of 4.
537+
'''
538+
if not pixel in resultats:
539+
resultats[pixel] = b((pixel & 16711680) >> 16) + b((pixel & 65280) >> 8) + b(pixel & 255)
540+
return resultats[pixel]
550541

551542
get_pix = self.XGetPixel
552543
pixels = [pix(get_pix(image, x, y)) for y in range(height) for x in range(width)]

0 commit comments

Comments
 (0)