I have written an if-elif statement, which I believe not be very efficient:
first_number = 1000
second_number = 700
switch = {
'upperRight': False,
'upperLeft': False,
'lowerRight': False,
'lowerLeft': False,
'middleLeft': False,
'middleRight': False,
'upperMiddle': False,
'lowerMiddle': False,
'middle': False
}
for i in range(first_number):
for j in range(second_number):
if pixel_is_black:
if i <= int(first_number/3) and j <= int(second_number/3):
switch['upperLeft'] = True
elif i <= int(first_number/3) and int(second_number/3) < j <= int(2*second_number/3):
switch['middleLeft'] = True
elif i <= int(first_number/3) and j > int(2*second_number/3):
switch['lowerLeft'] = True
elif int(first_number / 3) <= i < int(2 * first_number / 3) and j < int(second_number / 3):
switch['upperMiddle'] = True
elif int(first_number / 3) <= i < int(2 * first_number / 3) and int(second_number / 3) < j <= int(2 * second_number / 3):
switch['middle'] = True
elif int(first_number / 3) <= i < int(2 * first_number / 3) and j >= int(2 * second_number / 3):
switch['lowerMiddle'] = True
elif i >= int(2 * first_number / 3) and j <= int(2 * second_number / 3):
switch['upperRight'] = True
elif i >= int(2 * first_number / 3) and int(second_number / 3) < j <= int(2 * second_number / 3):
switch['middleRight'] = True
elif i >= int(2 * first_number / 3) and j >= int(2 * second_number / 3):
switch['lowerRight'] = True
for i in switch:
if(switch[i] == True):
print(i)
As you can see, the statement looks pretty ugly. Since number is big, it takes almost 2 seconds for the execution. In the loop, I am going through the pixels of an image. In the if-elif statement, I divide the image in 9 parts, and print the respective number, if the pixel is black in that area.
Is there any way I could lower the CPU time?
I tried this answer, but my statement conditions are different.
Thank you.