こんにちは、スクリプト内の関数の実行に時間がかかるという問題が発生しました。だから私はコードを最適化しようとしており、現在そのために 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