获取目录列表对 php 的性能有多大影响

发布于 2024-10-17 16:54:49 字数 221 浏览 2 评论 0原文

这次的问题非常简单,基本上我有一组文件夹,其中一些包含我希望在运行站点脚本时自动加载的文件。但是,我不想指定要自动加载的文件,因为我希望该过程是动态的,并且我希望能够动态创建和删除不同的文件。 因此,当然最简单的解决方案是获取目录中的文件夹列表并构建自动加载文件的路径,如果文件存在,则脚本包含它们。 但是,我的问题是这会对我的脚本的性能产生多大影响?它实际上是一个我想稍后发布的框架,因此性能是一个很大的问题。 有什么想法吗?

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 技术交流群。

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

发布评论

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

评论(4

沐歌 2024-10-24 16:54:49

您应该考虑简单地让 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.

夜灵血窟げ 2024-10-24 16:54:49

这取决于您的磁盘速度、文件系统和目录的大小。无论如何,必须动态加载目录来获取文件列表将比加载静态列表花费更长的时间;可以接受多少增加取决于您的要求。

如果可以的话,缓存该列表可能会有所帮助。

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.

幻梦 2024-10-24 16:54:49

自动加载很棒,尽管它不是“免费”的,但对性能的影响并不明显。当然,您可以衡量这一点并根据需要进行重构。

这是我的自动加载器:

spl_autoload_register(
    function ($className) {
        $possibilities = array(
            APPLICATION_PATH.'beans'.DIRECTORY_SEPARATOR.$className.'.php',
            APPLICATION_PATH.'controllers'.DIRECTORY_SEPARATOR.$className.'.php',
            APPLICATION_PATH.'helpers'.DIRECTORY_SEPARATOR.$className.'.php',
            APPLICATION_PATH.'models'.DIRECTORY_SEPARATOR.$className.'.php',
            APPLICATION_PATH.'views'.DIRECTORY_SEPARATOR.$className.'.php'
        );
        foreach (explode(PATH_SEPARATOR, ini_get('include_path')) as $k => $v) {
            $possibilities[] = $v.DIRECTORY_SEPARATOR.$className.'.php';
        }
        foreach ($possibilities as $file) {
            if (file_exists($file)) {
                require_once($file);
                return true;
            }
        }
        return false;
    }
);

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:

spl_autoload_register(
    function ($className) {
        $possibilities = array(
            APPLICATION_PATH.'beans'.DIRECTORY_SEPARATOR.$className.'.php',
            APPLICATION_PATH.'controllers'.DIRECTORY_SEPARATOR.$className.'.php',
            APPLICATION_PATH.'helpers'.DIRECTORY_SEPARATOR.$className.'.php',
            APPLICATION_PATH.'models'.DIRECTORY_SEPARATOR.$className.'.php',
            APPLICATION_PATH.'views'.DIRECTORY_SEPARATOR.$className.'.php'
        );
        foreach (explode(PATH_SEPARATOR, ini_get('include_path')) as $k => $v) {
            $possibilities[] = $v.DIRECTORY_SEPARATOR.$className.'.php';
        }
        foreach ($possibilities as $file) {
            if (file_exists($file)) {
                require_once($file);
                return true;
            }
        }
        return false;
    }
);
耀眼的星火 2024-10-24 16:54:49

这取决于。

尝试你的方法和措施。您以后随时可以添加缓存。或者求助于自动加载

It depends.

Try your approach and measure. You can always add caching later. Or resort to autoload.

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