Сколько стоит список каталогов влияет на производительность PHP

StackOverflow https://stackoverflow.com/questions/5035446

Вопрос

Очень простой вопрос на этот раз, в основном у меня есть группа папок, и некоторые из них содержат файлы, которые я хотел бы автозагрузку, когда я запускаю свои сайты скрипта.Однако я не хочу указывать, какие файлы на автозагрузку, потому что я хочу, чтобы процесс был динамическим, и я хочу иметь возможность создавать и удалять разные файлы на лету. Поэтому, конечно, самое простое решение состоит в том, чтобы получить список папок в каталогах и создать пути к файлам автоматической нагрузки, если файлы существуют, то скрипт включает в себя. Но мой вопрос в том, сколько будет повлиять на производительность моего сценария?Это на самом деле рамки, которые я хочу выпустить позже, так что производительность - это вполне проблема. Любые идеи?

Это было полезно?

Решение

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:

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;
    }
);

It depends.

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top