From 048cf66e997094c2cc31443c46cdaf7afec840df Mon Sep 17 00:00:00 2001 From: Theelgirl <43764914+Theelgirl@users.noreply.github.com> Date: Sat, 17 Oct 2020 15:33:12 +0000 Subject: [PATCH] Speedup by 40-45% --- fvid/fvid_cython.pyx | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/fvid/fvid_cython.pyx b/fvid/fvid_cython.pyx index a159843..30f3e20 100644 --- a/fvid/fvid_cython.pyx +++ b/fvid/fvid_cython.pyx @@ -1,12 +1,13 @@ -# distutils: language=c++ +# distutils: language=c # cython: boundscheck=False # cython: cdivision=True # cython: wraparound=False -cpdef str cy_get_bits_from_image(image): +cpdef cy_get_bits_from_image(image): cdef int width, height, x, y cdef str pixel_bin_rep - cdef tuple white_diff, black_diff, pixel + cdef (int, int, int) pixel, white_diff, black_diff + cdef (bint, bint, bint) truth_tuple width, height = image.size @@ -17,13 +18,13 @@ cpdef str cy_get_bits_from_image(image): for x in range(width): pixel = px[x, y] - pixel_bin_rep = "0" + pixel_bin_rep = "0" - # for exact matches + # for exact matches, indexing each pixel individually is faster in cython for some reason if pixel[0] == 255 and pixel[1] == 255 and pixel[2] == 255: - pixel_bin_rep = "1" + pixel_bin_rep = "1" elif pixel[0] == 0 and pixel[1] == 0 and pixel[2] == 0: - pixel_bin_rep = "0" + pixel_bin_rep = "0" else: white_diff = (abs(pixel[0] - 255), abs(pixel[1] - 255), abs(pixel[2] - 255)) # min_diff = white_diff @@ -31,10 +32,11 @@ cpdef str cy_get_bits_from_image(image): # if the white difference is smaller, that means the pixel is closer # to white, otherwise, the pixel must be black - if all((white_diff[0] < black_diff[0], white_diff[1] < black_diff[1], white_diff[2] < black_diff[2])): - pixel_bin_rep = "1" + truth_tuple = (white_diff[0] < black_diff[0], white_diff[1] < black_diff[1], white_diff[2] < black_diff[2]) + if all(truth_tuple): + pixel_bin_rep = "1" else: - pixel_bin_rep = "0" + pixel_bin_rep = "0" # adding bits bits += pixel_bin_rep