串行程序比并行程序更好地工作

时间:2018-12-03 06:31:54

标签: python opencv image-processing parallel-processing

我正在尝试比较图像处理中的串行和并行处理。 但是出于某种奇怪的原因,我的并行程序运行速度比串行程序慢得多。 谁能告诉我可能是什么问题?

并行代码: 我将每个任务划分为每个线程的单独功能。

def task2():
global t_end
print("Task 2 started...")

filter=[1/9,1/9,1/9,1/9,1/9,1/9,1/9,1/9,1/9]
for i in range(1, height - 2):
    for j in range(1, width - 2):
        # print i,j,img[i][j]
        ay = img[i - 1][j - 1] * float(filter[0])
        by = img[i][j - 1] * float(filter[1])
        cy = img[i + 1][j - 1] * float(filter[2])
        dy = img[i - 1][j] * float(filter[3])
        ey = img[i][j] * float(filter[4])
        fy = img[i + 1][j] * float(filter[5])
        gy = img[i - 1][j + 1] * float(filter[6])
        hy = img[i][j + 1] * float(filter[7])
        iy = img[i + 1][j + 1] * float(filter[8])

        Gy = ay + by + cy + dy + ey + fy + gy + hy + iy
        img_op2[i][j] = Gy
print("Active Threads :", threading.activeCount())
cv2.imshow('Blurred op', img_op2)
temp_time = time.time()
if (temp_time > t_end):
    t_end = temp_time

我已经以这种方式实现了线程:

t1=threading.Thread(target=task1)
t2=threading.Thread(target=task2)
t3=threading.Thread(target=task3)
t4=threading.Thread(target=task4)

t_start=time.time()
t1.start()
t2.start()
t3.start()
t4.start()

由于某种原因,我的并行代码的工作方式比串行代码慢。任何建议都会被采纳。

0 个答案:

没有答案