Comments on: PHP “require” Performance http://gazehawk.com/blog/php-require-performance/ Eye Tracking For Everyone - Usability Services & Software Sat, 10 Mar 2012 06:40:21 +0000 hourly 1 http://wordpress.org/?v=3.2.1 By: Dominevski http://gazehawk.com/blog/php-require-performance/#comment-10332 Dominevski Sat, 18 Feb 2012 15:40:18 +0000 http://www.gazehawk.com/blog/?p=107#comment-10332 I would say comparing "require_once" vs "require" is pointless. Its true that "require" is almost twice as fast than "require_once" but that cannot be tested, really, not in a real world scenario. If you know for sure you are only including a script ONCE, why would you use require_once? The whole idea of using require_once is when you dont know if and when you gonna need the script. When testing performance you test suite should ofcourse include same files multiple times right? Someone may argue that if your code is loading same files more than once, then it is a failure by design. I dont agree. For the same reason we use #ifndef in our C or C++ programs =) An example: Assume I have a utlity class with common stuff and it is used by both DB.php and Auth.php. Most of the time these two are NOT loaded in the same script but what if they do? Like the login script which uses DB backend. Using plain "include" or "require" will throw fatals if and when both DB and Auth are used in a script: Cannot redeclare bla bla... So there is only 2 options left: include_once or __autoload. Depending on your code I would say: - If you know your code is using the included file then require_once is better. - If the required file is most likely NOT used, then use __autoload - plain require/include should be used only when the included file does not contain any PHP code at all. Means you are loading static content or templates or whatever similar. I would say comparing “require_once” vs “require” is pointless.
Its true that “require” is almost twice as fast than “require_once”
but that cannot be tested, really, not in a real world scenario.

If you know for sure you are only including a script ONCE, why
would you use require_once? The whole idea of using require_once
is when you dont know if and when you gonna need the script.

When testing performance you test suite should ofcourse include
same files multiple times right? Someone may argue that if your
code is loading same files more than once, then it is a failure
by design. I dont agree. For the same reason we use #ifndef in
our C or C++ programs =)

An example:
Assume I have a utlity class with common stuff and it is used by
both DB.php and Auth.php. Most of the time these two are NOT
loaded in the same script but what if they do? Like the login
script which uses DB backend. Using plain “include” or “require”
will throw fatals if and when both DB and Auth are used in a
script: Cannot redeclare bla bla…

So there is only 2 options left: include_once or __autoload.

Depending on your code I would say:

- If you know your code is using the included file then require_once
is better.

- If the required file is most likely NOT used, then use __autoload

- plain require/include should be used only when the included
file does not contain any PHP code at all. Means you are loading
static content or templates or whatever similar.

]]>
By: opencart http://gazehawk.com/blog/php-require-performance/#comment-2676 opencart Fri, 05 Aug 2011 03:41:46 +0000 http://www.gazehawk.com/blog/?p=107#comment-2676 Can u test with include VS include_once VS require VS require_once ? Thanks. Amazegu. Can u test with
include VS include_once VS require VS require_once ?

Thanks.

Amazegu.

]]>
By: CS http://gazehawk.com/blog/php-require-performance/#comment-2127 CS Wed, 15 Jun 2011 06:35:37 +0000 http://www.gazehawk.com/blog/?p=107#comment-2127 If you can write a simple perl script to inline these require's as part of your build and deployment you will see a good increase in performance due to opcode caching + no file stat. If you can write a simple perl script to inline these require’s as part of your build and deployment you will see a good increase in performance due to opcode caching + no file stat.

]]>
By: using include/require/require_once http://gazehawk.com/blog/php-require-performance/#comment-1744 using include/require/require_once Tue, 17 May 2011 21:10:31 +0000 http://www.gazehawk.com/blog/?p=107#comment-1744 [...] morning I was reading through a article about the performance when we include files in php using require and [...] [...] morning I was reading through a article about the performance when we include files in php using require and [...]

]]>
By: Tony Arkles http://gazehawk.com/blog/php-require-performance/#comment-1740 Tony Arkles Tue, 17 May 2011 15:39:14 +0000 http://www.gazehawk.com/blog/?p=107#comment-1740 The relative values are not the only thing that matter. If the absolute difference between require() and require_once() is 100 microseconds, and you're only doing it 5 times, then it's completely irrelevant as far as optimizing a code base goes (guaranteed there's more significant wins to be gained elsewhere). The relative values are not the only thing that matter. If the absolute difference between require() and require_once() is 100 microseconds, and you’re only doing it 5 times, then it’s completely irrelevant as far as optimizing a code base goes (guaranteed there’s more significant wins to be gained elsewhere).

]]>
By: Joe http://gazehawk.com/blog/php-require-performance/#comment-1739 Joe Tue, 17 May 2011 15:27:21 +0000 http://www.gazehawk.com/blog/?p=107#comment-1739 How do you propose replacing instances of require_once with require? if ( ! class_exists()) ? How do you propose replacing instances of require_once with require? if ( ! class_exists()) ?

]]>
By: Brian Krausz http://gazehawk.com/blog/php-require-performance/#comment-1732 Brian Krausz Tue, 17 May 2011 06:18:59 +0000 http://www.gazehawk.com/blog/?p=107#comment-1732 That is left as an exercise to the reader :). However, I strongly suspect that spl_autoload_register will cause no notable overhead besides a few extra function calls (ditto with multiple autoload registers). That is left as an exercise to the reader :) .

However, I strongly suspect that spl_autoload_register will cause no notable overhead besides a few extra function calls (ditto with multiple autoload registers).

]]>
By: Nyro http://gazehawk.com/blog/php-require-performance/#comment-1731 Nyro Tue, 17 May 2011 06:16:14 +0000 http://www.gazehawk.com/blog/?p=107#comment-1731 Thanks for this enlightenment. What about a benchmark with spl_autoload_register ? And with multiple autoload functions ? Thanks for this enlightenment.
What about a benchmark with spl_autoload_register ?
And with multiple autoload functions ?

]]>
By: Brian Krausz http://gazehawk.com/blog/php-require-performance/#comment-1730 Brian Krausz Tue, 17 May 2011 06:12:04 +0000 http://www.gazehawk.com/blog/?p=107#comment-1730 FYI I edited your comment down to remove unnecessary info from the output. 1) I was running at a higher level of concurrency 2) I was including 30 files instead of 2 3) Are you using opcode caching? Not sure how that affects require vs. require_once, but it may. I'm sure there are more differences that affect the outcome of that test, but those are the most striking. FYI I edited your comment down to remove unnecessary info from the output.

1) I was running at a higher level of concurrency
2) I was including 30 files instead of 2
3) Are you using opcode caching? Not sure how that affects require vs. require_once, but it may.

I’m sure there are more differences that affect the outcome of that test, but those are the most striking.

]]>
By: Pknerd http://gazehawk.com/blog/php-require-performance/#comment-1729 Pknerd Tue, 17 May 2011 05:58:54 +0000 http://www.gazehawk.com/blog/?p=107#comment-1729 I am failing to get how did you perform test. I took a file named "main.php" and in which I included 2 files, r1 and r2.php. Both had sleep() for 3 seconds. The result of AB for both require and require_once are not SO different. What am I Missing? Stats are giving below: REQUIRE_ONCE <code> Concurrency Level: 10 Time taken for tests: 80.162736 seconds Complete requests: 100 Requests per second: 1.25 [#/sec] (mean) Time per request: 8016.273 [ms] (mean) Time per request: 801.627 [ms] (mean, across all concurrent requests) Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 0.3 0 1 Processing: 8004 8014 24.7 8006 8089 Waiting: 8003 8014 24.4 8006 8088 Total: 8004 8015 24.8 8006 8089 </code> REQUIRE <code> Concurrency Level: 10 Time taken for tests: 80.77928 seconds Complete requests: 100 Requests per second: 1.25 [#/sec] (mean) Time per request: 8007.793 [ms] (mean) Time per request: 800.779 [ms] (mean, across all concurrent requests) Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 1.6 0 9 Processing: 8004 8006 1.3 8006 8010 Waiting: 8002 8006 1.2 8006 8010 Total: 8004 8006 2.0 8006 8015 </code> Pls guide I am failing to get how did you perform test.

I took a file named “main.php” and in which I included 2 files, r1 and r2.php. Both had sleep() for 3 seconds. The result of AB for both require and require_once are not SO different. What am I Missing? Stats are giving below:

REQUIRE_ONCE


Concurrency Level: 10
Time taken for tests: 80.162736 seconds
Complete requests: 100
Requests per second: 1.25 [#/sec] (mean)
Time per request: 8016.273 [ms] (mean)
Time per request: 801.627 [ms] (mean, across all concurrent requests)

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.3 0 1
Processing: 8004 8014 24.7 8006 8089
Waiting: 8003 8014 24.4 8006 8088
Total: 8004 8015 24.8 8006 8089

REQUIRE


Concurrency Level: 10
Time taken for tests: 80.77928 seconds
Complete requests: 100
Requests per second: 1.25 [#/sec] (mean)
Time per request: 8007.793 [ms] (mean)
Time per request: 800.779 [ms] (mean, across all concurrent requests)

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 1.6 0 9
Processing: 8004 8006 1.3 8006 8010
Waiting: 8002 8006 1.2 8006 8010
Total: 8004 8006 2.0 8006 8015

Pls guide

]]>