使用python测量文件的读写速度

2024-09-30 18:35:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在使用python来读写大型图像(使用OpenCV和numpy的memmap函数)。具体地说,我正在制作图像金字塔。在

在python中,可以监视文件的当前读写速度吗?一、 e.类似移动平均值(MB/秒)。在

我不认为这很重要,但我的代码中最重要的部分是:

    for y in range(ysize):
        #Open a previously defined temp image file
        bigIMG = np.memmap('tempfile.bin', dtype='uint8', mode='r+', shape=(ysize,xsize,3))

        #Grap a chunk of the full image, save it, shrink it, 
        #and put it into the next lower level of the pyramid
        for x in range(xsize):
            chunk = bigIMG[y*block:(y+1)*block,x*block:(x+1)*block]
            cv2.imwrite('+str(x)+"_"+str(y)+".png",chunk)
            down = cv2.resize(chunk,None,fx=0.5, fy=0.5, interpolation = cv2.INTER_LINEAR)
            smallerIMG[(y*block)/2:((y+1)*block)/2,(x*block)/2:((x+1)*block)/2] = down

        #Flush changes to HDD
        del smallerIMG

如你所见,我正在从硬盘上写和读很多数据,我想监控这些操作的性能。在

谢谢!在


Tags: ofthein图像imageforrangeit
1条回答
网友
1楼 · 发布于 2024-09-30 18:35:22

有趣的是,你不一定要从硬盘上加载很多数据。至少,您正在加载和写入的数据量在很大程度上取决于您的系统配置。对文件系统参数的微小调整可能会在读取的数据量上产生巨大的差异。在

尤其是对于memmapped(great choice,BTW)文件,许多页面都只保存在RAM中。我试图找出是否真的有任何方法来查看缓存未命中(HDD页面输入/输出),但操作系统很好地保留了它的秘密。在

你可能想试试这样的方法:

import psutil

iocnt1 = psutil.disk_io_counters(perdisk=True)['sda1']
# do  something IO intensive
iocnt2 = psutil.disk_io_counters(perdisk=True)['sda1']

print 'Blocks written {0}'.format(iocnt2.write_count - iocnt1.write_count)
print 'Blocks read {0}'.format(iocnt2.read_count - iocnt1.read_count)

当然,您必须安装psutil模块,并将sda1更改为您的硬盘驱动器。在

不可能看到哪些写操作实际上是由您的进程引起的。由于操作系统的结构,这是不可能的。它将来自不同进程的读和写混合并匹配到一个队列中,之后就不可能说出是什么触发了写入或读取。在

但是,如果您的计算机没有执行任何特殊操作,通常的写入/读取IOPS非常低,因为大多数事情都发生在缓存中。当你改变你的算法时,至少你会看到变化。在

相关问题 更多 >