性能:是一次性读取所有文件更好,还是一遍又一遍地使用 b::fs 函数更好?
我在“读取一次,使用内存+文件指针”和“必要时读取”方法之间存在冲突。后者当然要容易得多(不需要额外的类来存储整个目录结构),但在我看来它更慢?我试图列出文件名和相对路径(以便编译器可以对它们执行所需的操作)。
一点澄清:我正在编写一个简单的构建系统,它读取项目文件,检查所有文件是否存在,并运行一些编译步骤。文件树是静态的,因此第一个选项不需要非常动态,只需要在程序每次运行时构建一次。谢谢
I'm conflicted between a "read once, use memory+pointers to files" and a "read when necessary" approach. The latter is of course much easier (no additional classes needed to store the whole dir structure), but IMO it is slower? I'm trying to list the filenames and relative paths (so the compiler can do with them what it needs to).
A little clarification: I'm writing a simple build system, that read a project file, checks if all files are present, and runs some compile steps. The file tree is static, so the first option doesn't need to be very dynamic and only needs to be built once every time the program is run. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以放心地假设操作系统无论如何都会缓存目录内容,因此通过文件系统 API 的访问将归结为内存操作。
所以你的问题的答案是“更快吗?”很可能是“不,无法衡量”。
OTOH,考虑到目录内容可能会随着时间的推移而改变,即使是在很短的时间内。因此,急切或懒惰地读取目录内容与其说是速度问题,不如说是语义问题。您可能会发现必须/不能读取整个目录,具体取决于您正在做什么。
You can safely assume that the operating system will cache the directory contents anyway, so that access through file system APIs will come down to memory operations.
So the answer to your question "is it faster?" is likely "No, not measurably".
OTOH, consider that a directories contents can change over time, even in very short time. Thus, reading directory content eagerly or lazyily is not so much a question of speed, but of semantics. It may be that you find that you must/must not read the entire directory, depending on what you are doing.