python opencv 性能测量和改进技术

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/TingHW/article/details/84504775
# cv2.getTickFrequency函数返回时钟频率或者每秒钟的时钟循环数
# cv2.getTickCount 函数返回从一个参考时间(比如机器开机的时间)开始到这个函数被调用的时间之间的时钟循环数量。
import cv2
import numpy as np
img1 = cv2.imread('messi5.jpg')

e1 = cv2.getTickCount()
for i in range(5,49,2):
    img1 = cv2.medianBlur(img1,i)
e2 = cv2.getTickCount()
t = (e2-e1)/cv2.getTickFrequency()
print(t)

0.0002125430419413002
x = 5
# IPython提供了一个魔法指令%timeit来干这个,它运行代码若干次来得到准确结果,很适合用来测量单行代码。
%timeit y=x**2
289 ns ± 10.3 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
%timeit y=x*x
45 ns ± 1.07 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
 z = np.uint8([5])
%timeit y=z*z
The slowest run took 11.67 times longer than the fastest. This could mean that an intermediate result is being cached.
3.1 µs ± 4.39 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit y=np.square(z)
414 ns ± 9.65 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
# Python标量运算时比Numpy标量运算要快的。所以对于包含1到两个元素的运算,Python标量要比Numpy数组要快。Numpy在数组尺寸有点大的时候占优势。

猜你喜欢

转载自blog.csdn.net/TingHW/article/details/84504775
今日推荐