对于目录结构树的遍历算法有很多文件 [英] Tree traversal algorithm for directory structures with a lot of files

查看:111
本文介绍了对于目录结构树的遍历算法有很多文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在递归的目录结构遍历,什么是最有效的算法来使用,如果你有比目录多个文件?我发现,使用深度优先遍历时,似乎当有大量的文件,在给定的目录中需要更长的时间。在这种情况下更有效地做广度优先遍历的工作?我没有办法来分析这两种算法的那一刻让你的见解是非常受欢迎的。

When recursively traversing through a directory structure, what is the most efficient algorithm to use if you have more files than directories? I notice that when using depth-first traversal, it seems to take longer when there are a lot of files in a given directory. Does breadth-first traversal work more efficiently in this case? I have no way to profile the two algorithms at the moment so your insights are very much welcome.

编辑:在回答alphazero的评论,我使用的是Linux机器上的PHP

In response to alphazero's comment, I'm using PHP on a Linux machine.

推荐答案

这是有道理的广度优先将工作做得更好。当你进入你的根文件夹,您可以创建你需要处理的项目清单。一些这些项目是文件,有些是目录。

It makes sense that breadth-first would work better. When you enter your root folder, you create a list of items you need to deal with. Some of those items are files and some are directories.

如果您使用广度优先,你会处理该目录中的文件,移动到孩子的一个目录中之前忘记他们。

If you use breadth-first, you would deal with the files in the directory and forget about them before moving on to one of the child directories.

如果您使用深度优先,你需要保持不断增长的文件列表,处理以后,你更深入钻到。这将使用更多的内存来维护您的文件列表来处理,可能导致更多的页面错误,等等...

If you use depth-first, you need to keep growing a list of files to deal with later as you drill deeper down. This would use more memory to maintain your list of files to deal with, possibly causing more page faults, etc...

此外,你需要去通过新的项目清单反正要弄清楚哪些是你可以深入到目录。你会需要经过同样的清单(减去目录)再次当你已经得到了处理文件的地步。

Plus, you'd need to go through the list of new items anyway to figure out which ones are directories that you can drill into. You would need to go through that same list (minus the directories) again when you've gotten to the point of dealing with the files.

这篇关于对于目录结构树的遍历算法有很多文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆