开发者

Why do people say require_once is slower than require? [duplicate]

开发者 https://www.devze.com 2023-04-03 05:00 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Why is require_once so bad to use?
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Why is require_once so bad to use?

When PHP is inc开发者_JS百科luding a file I assume it stores the file name inside a array.

Then, when including another file, it will check that array to see if it's already included, right?

So what's the big deal here? Why are people so scared of this array check? It's not like you're including milions of files...

in_array checks are made all the time. I use them in almost all functions :)


I am not certain who the "people" are that say this, but I believe it to be one of many micro-optimization myths that pervade any language.

Here is an article which benchmarks the methods: http://arin.me/blog/php-require-vs-include-vs-require_once-vs-include_once-performance-test

Your mileage may vary, but I doubt you will see any significant performance gains by avoiding x_once functions. Use the language constructs that are right for the situation and you aren't doing anything wrong. x_once might be a sign that you need to reconsider your project's organization or consider using autoloader, but it isn't eval...


require_once() has two issues, if you ignore the performance issue the second one is also important. If you are using require_once() it implies you may require the same file more than once. This is inherently wrong or faulty design. Now the performance part, for web an optimised app will do nothing. If you understand that serving a static HTML file or cached content is faster than serving a PHP file you will understand why people say using require_once() is slower.


As a matter of fact, for the sensible planned application, functions like require_once are useless.

For the slowness - it's negligible.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号