你好,我有一个问题,在我的脚本中的函数需要很长时间才能执行。所以我正在尝试优化代码,我目前正在使用cProfile和Pstats来实现这一点。你知道吗
当我执行一个函数时,大约需要0.7秒到1+秒来完成。在Windows上始终提供最长持续时间的项目是:
Sun Mar 10 13:55:02 2019 profiles/getColors.profile
657 function calls in 0.535 seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.714 0.714 0.714 0.714 {built-in method _winapi.WaitForMultipleObjects}_winapi.WaitForMultipleObjects}
在Linux上:
2 1.013 0.506 1.013 0.506 {built-in method posix.read}
因此,我将假设它与线程有关,但我从不创建任何线程,而我的其他函数几乎不需要花费任何时间来完成,比如~0.1秒,所以我的问题是,为什么执行此代码需要这么长的时间:
def getColors(image, rows, columns, sectionedPixel):
# Flooring so we do not get decimal numbers
sectionColumns = math.floor(width / columns)
sectionRows = math.floor(height / rows)
colorValues = [0, 0, 0, 0]
leftRGBVal = [0, 0, 0]
rightRGBVal = [0, 0, 0]
topRGBVal = [0, 0, 0]
botRGBVal = [0, 0, 0]
# LEFT SIDE
getRiLeSideColor(image, 0, 10, leftRGBVal)
# RIGHT SIDE
getRiLeSideColor(image, width - 10, width, rightRGBVal)
# TOP SIDE
getToBoSideColor(image, 0, 10, topRGBVal)
# BOTTOM SIDE
getToBoSideColor(image, height - 10, height, botRGBVal)
colorValues[0] = leftRGBVal
colorValues[1] = rightRGBVal
colorValues[2] = topRGBVal
colorValues[3] = botRGBVal
return colorValues
CProfile的完整日志: https://pastebin.com/jAA5FkPZ
完整代码: https://gist.github.com/Patrick265/592a7dccba4660a4e4210ddd5e9974eb
如果我只是运行脚本并检查两个主要调用
retrieveScreen(...)
和getColors(...)
的计时,我会看到平均计时:我猜你的屏幕盖依赖于操作系统,只是需要时间。你知道吗
从几个快速测试中,我认为应该更快地获得屏幕的
PixelAccess
,就像这样,只使用裸PIL
:编辑:完整示例
这个(python3)代码只使用
PIL
。老实说,我不确定枕头访问屏幕的实际方法,但它比你以前的方法稍微快一点。我认为你正在尝试实现一些像屏幕背光,这将需要更新相当快,不知道这是否是足够的你的目的。但是,可以根据需要随意使用代码:示例性输出,仅用于演示目的:
相关问题 更多 >
编程相关推荐