【问题标题】:Why does this code take so long to be executed? - Python为什么这段代码需要这么长时间才能执行? - Python
【发布时间】: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理解。

谢谢。

【问题讨论】:

  • 我不认为forwhile 有什么不同。您能否尝试按照此处的示例将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


【解决方案1】:

而不是这个:

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
    #snip
if not same:
    #snip

你可以这样做:

same=np.array_equal(self.__sub_images[i][j].get_image(), to_compare_image.get_sub_images()[k][l].get_image())
if same:
    #snip
else:
    #snip

这比以前使用了更少的 if 分支。

【讨论】:

  • 虽然风格更好,但这是一个微不足道的变化;它不是表演中有意义的组成部分。微优化一个简单的布尔检查是没有意义的,除非它是代码中最热门的部分(它不是)。
  • 我不认为仅仅删除 if 会提高效率。知道为什么 40+ 的复杂度需要这么长时间吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-22
  • 1970-01-01
  • 2012-04-29
相关资源
最近更新 更多