【发布时间】:2016-10-16 00:53:10
【问题描述】:
我在 Python 上编写了这个代码。如果输入只是 40x40(它用于使用 numpy 进行图像处理),则完成的时间太长了。它的行为如下:有一个包含对象的数组(它有一个 'image' 属性,它是一个 numpy 数组),然后我检查该对象是否在另一个数组中的某个位置,然后从第一个数组中获取下一个数组和重复这个过程,直到我检查了是否都在另一个数组中:
#__sub_images is the array containing the objects to be compared
#to_compare_image is the image that is received as parameter to check if the objects are in there.
#get_sub_images() is just to retrieve the objects from the array from the received array to find.
#get_image() is the method that retrieves the attribute from the objects
same = True
rows_pixels = 40 #problem size
cols_pixels = 40 #problem size
i = 0 #row index to move the array containing the object that must be checked if exist
j = 0 #col index to move the array containing the object that must be checked if exist
k = 0 #row index to move the array where will be checked if the object exist
l = 0 #col index to move the array where will be checked if the object exist
while i < len(self.__sub_images) and k < len(to_compare_image.get_sub_images()) and l < len(to_compare_image.get_sub_images()[0]):
if not np.array_equal(self.__sub_images[i][j].get_image(), to_compare_image.get_sub_images()[k][l].get_image()):
same = False
else:
same = True
k = 0
l = 0
if j == len(self.__sub_images[0]) - 1:
j = 0
i += 1
else:
j += 1
if not same:
if l == len(to_compare_image.get_sub_images()[0]) - 1:
l = 0
k += 1
else:
l += 1
我设法只使用while 对其进行编码,而不是我以前使用的 4 个for-loops。为什么还需要这么长时间?这是正常的还是有什么问题?复杂度应该是 x 而不是 x⁴
未包含的代码只是getter,希望你能用开头的#notes理解。
谢谢。
【问题讨论】:
-
我不认为
for和while有什么不同。您能否尝试按照此处的示例将while语句替换为预先计算的值:eval.in/661185 这是为了避免重新计算列表长度。看看这有多大帮助。 -
如果你像以前一样使用 4 个嵌套循环 (for),每个 2D 数组 2 个循环,就会有很大的不同。复杂度变成 x⁴,一会儿就变成 x。
-
好的。我的建议有帮助吗?也应该应用于
if not same之后的行。 -
我不明白你的意思,很难将其更改为预先计算的值,因为有几个类,直到这些值被正确填充,我认为它不会改变任何东西你,因为它们仍然是相同的值。
-
从上面的代码中,我最好的猜测是像
to_compare_image.get_sub_images()这样的操作可能需要很长时间。这些在每个循环中运行。因此,如果您查看我在此处发布的示例 (eval.in/661185),您会将这些检查的计算移到while循环之外。出于同样的原因,在if not same语句之后,将to_compare_image.get_sub_images()[0])替换为其计算值。
标签: python performance loops