获取目录列表对 php 的性能有多大影响
这次的问题非常简单,基本上我有一组文件夹,其中一些包含我希望在运行站点脚本时自动加载的文件。但是,我不想指定要自动加载的文件,因为我希望该过程是动态的,并且我希望能够动态创建和删除不同的文件。 因此,当然最简单的解决方案是获取目录中的文件夹列表并构建自动加载文件的路径,如果文件存在,则脚本包含它们。 但是,我的问题是这会对我的脚本的性能产生多大影响?它实际上是一个我想稍后发布的框架,因此性能是一个很大的问题。 有什么想法吗?
Very simple question this time, basically I have a group of folders and some of them contain files that I would like to autoload whenever I run my sites script. However I do not want to have to specify what files to autoload because I want the process to be dynamic and I want to be able to create and delete different files on the fly.
So of course the easiest solution is to get a list of folders in the directories and build the paths to the auto load files, if the files exist then the script includes them.
But, my question is how much will that affect the performance of my script? Its actually a framework which I want to release later on so performance is quite the issue.
Any Ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该考虑简单地让 PHP 自动加载您的类。
如果这不起作用,那么您几乎只剩下目录扫描解决方案,并且您不应该真正关心性能损失。如果您想要该功能,您将忍受成本。
一般来说,您不应该过分强调 PHP 的性能。如果当你的框架完成后它成为一个问题,请重新访问它。您很可能会发现,通过在框架中实现良好的缓存系统,您现在所获得的任何性能增益/损失都变得毫无意义。
请参阅过早优化。
You should consider simply letting PHP autoload your classes.
If that won't work, then you're pretty much left with the directory-scanning solution, and you shouldn't really care about performance penalties. If you want the functionality, you'll put up with the costs.
Generally you shouldn't stress overmuch about performance in PHP anyways. If it becomes an issue when your framework is complete, revisit it. Chances are you'll find whatever performance gains/losses you incur now are rendered moot by implementing a good caching system in your framework.
See Premature Optimization.
这取决于您的磁盘速度、文件系统和目录的大小。无论如何,必须动态加载目录来获取文件列表将比加载静态列表花费更长的时间;可以接受多少增加取决于您的要求。
如果可以的话,缓存该列表可能会有所帮助。
It depends on your disk speed, file system, and the size of the directory. No matter what, having to dynamically load the directory to get the list of files will take longer than loading a static list; how much of an increase is acceptable depends on your requirements.
If it's an option, caching that list could help.
自动加载很棒,尽管它不是“免费”的,但对性能的影响并不明显。当然,您可以衡量这一点并根据需要进行重构。
这是我的自动加载器:
Autoloading is great although it's not "free", the performance hit is not noticeable. Of course you can measure this and refactor if needed.
Here's my autoloader:
这取决于。
尝试你的方法和措施。您以后随时可以添加缓存。或者求助于自动加载。
It depends.
Try your approach and measure. You can always add caching later. Or resort to autoload.