如何优化Python从外部硬盘读取文件的速度?

发布于 2025-01-13 04:03:52 字数 706 浏览 0 评论 0原文

我需要处理来自外部硬盘的约 200 个文件夹,其中包含 300 张图片 (205 kb)。

我在线程中有以下循环。

ffs=FileFrameStream(lFramePaths).start()

#___While Loop through the frames____

image,path = ffs.read()

while ffs.more(): #While there is frames in the Queue to read
        try:
            img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 
            #some more operations....
        except:
            print(f"Erorr in picture:{path}")
            image,path=ffs.read()
            count+=1
            continue
     image,path=ffs.read()
     count+=1
ffs.stop()

对于 1 到 30-40 个文件夹,代码运行速度很快。一个文件夹大约需要0.5秒,20个13.20秒,但如果我要分析200个文件夹,需要500-600秒。所以我不知道我做错了什么或者如何提高代码的性能。

我感谢您能提供的任何帮助。

爱德华多

I need to process about 200 folders containing 300 pictures (205 kb) from an external HD.

I have the following loop within a thread.

ffs=FileFrameStream(lFramePaths).start()

#___While Loop through the frames____

image,path = ffs.read()

while ffs.more(): #While there is frames in the Queue to read
        try:
            img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 
            #some more operations....
        except:
            print(f"Erorr in picture:{path}")
            image,path=ffs.read()
            count+=1
            continue
     image,path=ffs.read()
     count+=1
ffs.stop()

The code runs fast for 1 to 30-40 folders. One folder takes around 0.5s, and for 20 13.20s, but if I want to analyse the 200 folders, it takes 500-600 s. So I don't know what I'm doing wrong or how I can increase the performance of the code.

I appreciate any help you can provide.

Eduardo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

地狱即天堂 2025-01-20 04:03:52

您可能会看到操作系统文件缓存的影响。当解析 200 个文件时,它可能会超出容量,并且实际的流传输直接从磁盘而不是 RAM 完成。

检查您的操作系统文件缓存容量是否小于 200 个文件大小的总和,或者是否可以缓存外部驱动器。当数据开始无法容纳缓存时,性能会呈指数下降,除非磁盘驱动器几乎与 RAM 一样快(我猜不是)。

You are probably seeing effects of your operating-system's file cache. When parsing 200 files, it may go out of capacity and the actual streaming is done directly from disk instead of RAM.

Check if your OS file cache capacity is less than sum of 200 files' sizes or if it can cache the external drive or not. When data is beginning to not fit the cache, the performance drops exponentially, unless the disk drive is nearly as fast as RAM (I guess its not).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文