Speeding up require_once()
Moderator: General Moderators
Speeding up require_once()
Hello,
I've tested that connecting 20 scirpts with 1000 lines each using require_once() taking up 0.15 seconds on pretty strong server. Same with 1 file with 20000 lines. Is there a way to speed up this process? I need more like 0.01... Is it possible?
Thanks
I've tested that connecting 20 scirpts with 1000 lines each using require_once() taking up 0.15 seconds on pretty strong server. Same with 1 file with 20000 lines. Is there a way to speed up this process? I need more like 0.01... Is it possible?
Thanks
- AKA Panama Jack
- Forum Regular
- Posts: 878
- Joined: Mon Nov 14, 2005 4:21 pm
I think you will find it's not a require_once problem but the shear SIZE in lines that need to be processed by PHP. 20,000 lines of PHP is damned huge and going to take some time for the interpreter to process each time.
You really need to be running something that large on a server with an accelerator/precompiler installed.
You really need to be running something that large on a server with an accelerator/precompiler installed.
- thomas777neo
- Forum Contributor
- Posts: 214
- Joined: Mon Mar 10, 2003 6:12 am
- Location: Johannesburg,South Africa
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
One of the goals in PHP is to load and execute only a reasonably small part of the whole codebase on each request. It is in fact a Web goal/premise in that websites are divided into requestable chunks (i.e. pages). You really need to look at those 20,000 lines and see how you can break it up. I would suggest implementing a controller architecture. That is the most common solution to this problem.amir wrote:i need speed up this process
(#10850)
yup... time to dive into design patterns, my friend.
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
If you can install opcode cachers on your server then do. Zend Optimizer and APC are both very popular. You have to pay for Zend though.
Also, I highly highly recommend you use a profiler (APD?) to find specific bottlenecks in your code if your problem persists.
Last point, the talks on performance are very good.
Also, I highly highly recommend you use a profiler (APD?) to find specific bottlenecks in your code if your problem persists.
Last point, the talks on performance are very good.
A piece of software I've written is long a little more than 3000 code lines (without comments) - one main file and ~20 include files.One of the goals in PHP is to load and execute only a reasonably small part of the whole codebase on each request. It is in fact a Web goal/premise in that websites are divided into requestable chunks (i.e. pages).
I find that combining all code into one large file speeds the execution by about 40msec, which is about 40% percent speedup for my particular app.
Somehow it looks that multiple including really does slow the execution.
Can anyone tell me how bytecode caches would work best - on multiple include files or on one large one?
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
require_once and include_once are slow, replace them with require and include. You can write a wrapping function to emulate the onceness:
Make sure that none of your code minds being required from a function though. I found this out recently, This is a problem:
Code: Select all
function loadFile($file)
{
static $loads = array();
if (!$loads[$file]) require $file;
$loads[$file] = true;
}Code: Select all
// Main.php
loadFile('Foo.php');
var_dump($foo); // NULL!!
loadFile('Bar.php');
var_dump($bar); // 10Code: Select all
// Foo.php
$foo = 10;Code: Select all
// Bar.php
$GLOBALS['bar'] = 10;Hmm, I doubt that the _once() functions do something much different than your code, can you support your claim somehow? Profiling data?ole wrote:require_once and include_once are slow, replace them with require and include. You can write a wrapping function to emulate the onceness:
Edit:
In fact, I'm sure it doesn't: http://lxr.php.net/source/pecl/apc/apc_zend.c#142
Last edited by Mordred on Thu Oct 19, 2006 6:44 am, edited 1 time in total.
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK