做个简单测试:
In [16]: def calc():
...: frame = np.random.randint(0, 256, (2160, 3840, 3)).astype(np.uint8)
...: kernel = np.ones((31,31), np.uint8)
...: cv2.erode(frame, kernel, iterations=10)
...:
In [17]: %timeit calc()
195 ms ± 1.69 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
然后16线程执行下:
In [18]: def multi_thread():
...: thres = [threading.Thread(target=calc) for _ in range(16)]
...: [x.start() for x in thres]
...: [x.join() for x in thres]
...:
In [19]: %timeit multi_thread()
1.37 s ± 18.3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
同时跑的时候看了下cpu占用率是240%左右。
有点奇怪。加速了,但这个加速比不太对。
再测试下多进程的情况:
In [21]: def multi_proc():
...: thres = [multiprocessing.Process(target=calc) for _ in range(16)]
...: [x.start() for x in thres]
...: [x.join() for x in thres]
...:
In [22]: %timeit multi_proc()
614 ms ± 20.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
好多了但还是有3倍差距,我这是16核cpu,感觉差距不应该有这么大...
anyway,opencv release gil的行为看来没有我想的那么乐观。
【 在 CKevin 的大作中提到: 】
: 你是说,比如
: def calc(image):
: # cv2 and np here
: ...................
[upload=1][/upload]
--
修改:lvsoft FROM 180.158.52.*
FROM 180.158.52.*