首页 > 解决方案 > 为什么当我设置 useOptimized false 时执行时间更少

问题描述

python:3.8.0 openCV:4.2.0 环境:pyCharm

当我 setUseOptimized 为 false 时,执行时间比为 true 时要短。为什么?它应该更慢。发生了什么?这是我的代码和输出。

import cv2

print(cv2.useOptimized())

img = cv2.imread('images/ship.bmp')

e1 = cv2.getTickCount()
res = cv2.medianBlur(img, 49)
e2 = cv2.getTickCount()
print((e2 - e1) / cv2.getTickFrequency())

cv2.setUseOptimized(False)
print(cv2.useOptimized())
e1 = cv2.getTickCount()
res = cv2.medianBlur(img, 49)
e2 = cv2.getTickCount()
print((e2 - e1) / cv2.getTickFrequency())
True
0.000351
False
0.0002813

标签: pythonopencv

解决方案


此处的 openCV 文档中,您可能会观察到这样的结果有几个原因

  1. 您的平台不支持任何优化代码(如 SSE、AVX)
  2. 您正在执行的操作不包括优化代码。
  3. 您正在执行的操作不会暴露优化的好处。即执行单个中值模糊是一项相对简单的任务,不会消耗太多资源。
  4. 您还没有重复足够的测试以获得可靠的结果。
  5. 您在其上执行脚本的系统正在运行导致结果偏差的其他进程。

在我的机器上,我运行了以下命令:

import cv2
from enum import Enum


class CPUFeatures(Enum):
    CPU_MMX = 1
    CPU_SSE = 2
    CPU_SSE2 = 3
    CPU_SSE3 = 4
    CPU_SSSE3 = 5
    CPU_SSE4_1 = 6
    CPU_SSE4_2 = 7
    CPU_POPCNT = 8
    CPU_FP16 = 9
    CPU_AVX = 10
    CPU_AVX2 = 11
    CPU_FMA3 = 12
    CPU_AVX_512F = 13
    CPU_AVX_512BW = 14
    CPU_AVX_512CD = 15
    CPU_AVX_512DQ = 16
    CPU_AVX_512ER = 17
    CPU_AVX_512IFMA512 = 18
    CPU_AVX_512IFMA = 18
    CPU_AVX_512PF = 19
    CPU_AVX_512VBMI = 20
    CPU_AVX_512VL = 21
    CPU_AVX_512VBMI2 = 22
    CPU_AVX_512VNNI = 23
    CPU_AVX_512BITALG = 24
    CPU_AVX_512VPOPCNTDQ = 25
    CPU_AVX_5124VNNIW = 26
    CPU_AVX_5124FMAPS = 27
    CPU_NEON = 100
    CPU_MSA = 150
    CPU_VSX = 200
    CPU_VSX3 = 201
    CPU_AVX512_SKX = 256
    CPU_AVX512_COMMON = 257
    CPU_AVX512_KNL = 258
    CPU_AVX512_KNM = 259
    CPU_AVX512_CNL = 260
    CPU_AVX512_CLX = 261
    CPU_AVX512_ICL = 262
    CPU_MAX_FEATURE = 512


def run_optimised(img, repeat):
    cv2.setUseOptimized(True)
    start = cv2.getTickCount()
    for i in range(repeat):
        cv2.medianBlur(img, 49)
    # Get performance count
    time = (cv2.getTickCount() - start) / cv2.getTickFrequency()
    return time


def run_non_optimised(img, repeat):
    cv2.setUseOptimized(False)
    start = cv2.getTickCount()
    for i in range(repeat):
        cv2.medianBlur(img, 49)
    # Get performance count
    time = (cv2.getTickCount() - start) / cv2.getTickFrequency()
    return time


if __name__ == "__main__":

    print("Supported CPU features")
    for cpu_feature in CPUFeatures:
        supported = cv2.checkHardwareSupport(int(cpu_feature.value))
        print("{} is {} supported".format(cpu_feature, "" if supported else "not"))

    print("\nRunning tests\n")
    img = cv2.imread('C:/tiger.jpg')

    repeats = 3
    iterations = 100
    total_optimised = 0
    total_non_optimised = 0
    for i in range(repeats):
        print("Test # {}".format(i))
        optimised = run_optimised(img, iterations)
        non_optimised = run_non_optimised(img, iterations)
        print("Optimised: {}".format(optimised))
        print("Non-optimised: {}".format(non_optimised))
        total_optimised += optimised
        total_non_optimised += non_optimised

    print("Final - optimised: {}".format(optimised))
    print("Final - non-optimised: {}".format(non_optimised))=

这产生了以下输出:

>python test.py
Supported CPU features
CPUFeatures.CPU_MMX is  supported
CPUFeatures.CPU_SSE is  supported
CPUFeatures.CPU_SSE2 is  supported
CPUFeatures.CPU_SSE3 is  supported
CPUFeatures.CPU_SSSE3 is  supported
CPUFeatures.CPU_SSE4_1 is  supported
CPUFeatures.CPU_SSE4_2 is  supported
CPUFeatures.CPU_POPCNT is  supported
CPUFeatures.CPU_FP16 is  supported
CPUFeatures.CPU_AVX is  supported
CPUFeatures.CPU_AVX2 is  supported
CPUFeatures.CPU_FMA3 is  supported
CPUFeatures.CPU_AVX_512F is not supported
CPUFeatures.CPU_AVX_512BW is not supported
CPUFeatures.CPU_AVX_512CD is not supported
CPUFeatures.CPU_AVX_512DQ is not supported
CPUFeatures.CPU_AVX_512ER is not supported
CPUFeatures.CPU_AVX_512IFMA512 is not supported
CPUFeatures.CPU_AVX_512PF is not supported
CPUFeatures.CPU_AVX_512VBMI is not supported
CPUFeatures.CPU_AVX_512VL is not supported
CPUFeatures.CPU_AVX_512VBMI2 is not supported
CPUFeatures.CPU_AVX_512VNNI is not supported
CPUFeatures.CPU_AVX_512BITALG is not supported
CPUFeatures.CPU_AVX_512VPOPCNTDQ is not supported
CPUFeatures.CPU_AVX_5124VNNIW is not supported
CPUFeatures.CPU_AVX_5124FMAPS is not supported
CPUFeatures.CPU_NEON is not supported
CPUFeatures.CPU_MSA is not supported
CPUFeatures.CPU_VSX is not supported
CPUFeatures.CPU_VSX3 is not supported
CPUFeatures.CPU_AVX512_SKX is not supported
CPUFeatures.CPU_AVX512_COMMON is not supported
CPUFeatures.CPU_AVX512_KNL is not supported
CPUFeatures.CPU_AVX512_KNM is not supported
CPUFeatures.CPU_AVX512_CNL is not supported
CPUFeatures.CPU_AVX512_CLX is not supported
CPUFeatures.CPU_AVX512_ICL is not supported
CPUFeatures.CPU_MAX_FEATURE is not supported

Running tests

Test # 0
Optimised: 2.210887199547867
Non-optimised: 2.659088088298851
Test # 1
Optimised: 2.2174625162069614
Non-optimised: 2.341818393377586
Test # 2
Optimised: 2.235847859572912
Non-optimised: 2.352969891067055
Final - optimised: 6.664197575327741
Final - non-optimised: 7.353876372743493

Process finished with exit code 0

推荐阅读