1

我正在与 python 多处理模块并行运行一个函数。此函数将图像作为输入,并输出一些测量值。问题是,我比较了 time.time() 与 time.clock() 的结果,看起来墙时间比 CPU 时间长得多。像 240 秒对 0.22 秒!

如果我理解正确,这意味着 CPU 在执行我的功能上花费的时间不到 1 秒,而且在大多数情况下,我都在等待读/写/磁盘等。这听起来正常吗?我用的是公用的超算中心(虽然很强大),不知道是不是因为其他用户也在用cpu节点,所以我要等待?

以下是我的代码,您能否指出代码的任何部分是否可能是原因?非常感谢!

import cv2
import glob
from joblib import Parallel,delayed   # this is for parallel computing
import time as t
from Function_ImageMeasure_sz import salinency, color_HSV,rot_balance

mypath='C:/Research/Data/Images/*.jpg'
# define function that will be executed in parallel
def get_measurement(file_name):
    img = cv2.imread(file_name)  # read image
# the following steps are computing some metrics over the input image
    contours,ind,centroid=salinency(img)
    d_rot,balance=rot_balance(img,centroid)
    color_rank, color_tone,bright_stat,saturate_stat,clear_dull,symmetry=color_HSV(img)
    result=[d_rot,balance,centroid,color_rank,color_tone,bright_stat,saturate_stat,clear_dull,symmetry]
    return result

# call function 
files=glob.glob(mypath) # input all images in the same folder
njob=2 # define how many threads/pipelines in parallel

t1=t.clock()
t3=t.time()
# this is implement the function in parallel
results=Parallel(n_jobs=njob,backend='multiprocessing')(map(delayed(get_measurement),files)) 
t2=t.clock()
t4=t.time()

# output results
print 'processing %s image takes %s seconds (wall-time), %s seconds (cpu-time) with %s threads/cores'%(len(files),t4-t3,t2-t1,njob)`
4

0 回答 0