我正在使用os.walk
构建数据存储的映射(此映射稍后将在我正在构建的工具中使用)
这是我当前使用的代码:
def find_children(tickstore):
children = []
dir_list = os.walk(tickstore)
for i in dir_list:
children.append(i[0])
return children
我对此做了一些分析:
dir_list = os.walk(tickstore)
立即运行,如果我对dir_list
不做任何操作,则此函数立即完成。
它在dir_list
上迭代需要很长时间,即使我没有append
任何东西,只要在它上迭代就需要时间。
Tickstore
是一个大数据存储,有大约10000个目录。
目前,完成此功能大约需要35分钟。
有没有办法加快速度?
我已经研究了os.walk
的替代方案,但它们似乎都没有在速度方面提供太多优势。
python2.7中的一种优化方法,用
scandir.walk()
代替os.walk()
,参数完全相同。PS:正如注释中提到的@reconp,
scandir
需要在python2.7中使用之前安装。os.walk
当前非常慢,因为它首先列出目录,然后对每个条目执行stat
操作,以查看它是目录还是文件。PEP 471中提出了一个改进,在Python 3.5中很快就会出现。同时,您可以使用scandir包在Python 2.7中获得相同的好处
是:使用Python 3.5(它目前仍然是RC,但是should be out momentarily)。在Python 3.5中,
os.walk
被重写以提高效率。这项工作是PEP 471的一部分。
摘自政治公众人物:
相关问题 更多 >
编程相关推荐