Page 1 of 1
Speeding up require_once()
Posted: Wed Oct 18, 2006 3:56 pm
by amir
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
Posted: Wed Oct 18, 2006 4:05 pm
by AKA Panama Jack
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.
Posted: Wed Oct 18, 2006 4:06 pm
by feyd
Optimize the code (more)? Break it into smaller chunks? Preparse them? Its difficult to say without seeing them.
Posted: Wed Oct 18, 2006 4:09 pm
by thomas777neo
There are many factors to consider. The coding quality, sql methodologies, database setup etc etc.
I feel that your current response time is good enough, unless you have many users...
What are you intending to do?
Posted: Wed Oct 18, 2006 4:26 pm
by timvw
Make sure the file is required/included once, and only once.. And use require/include instead of the *_once version...
Posted: Wed Oct 18, 2006 4:28 pm
by RobertGonzalez
Can the called scripts be smallerized?
Posted: Wed Oct 18, 2006 4:35 pm
by amir
i need speed up this process
Posted: Wed Oct 18, 2006 4:48 pm
by Christopher
amir wrote:i need speed up this process
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.
Posted: Wed Oct 18, 2006 5:08 pm
by Luke
yup... time to dive into
design patterns, my friend.
Posted: Wed Oct 18, 2006 5:11 pm
by amir
Now i have got an idea atleast,
Thanks all of you!
Posted: Wed Oct 18, 2006 5:25 pm
by Ollie Saunders
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.
Posted: Thu Oct 19, 2006 5:50 am
by Mordred
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).
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.
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?
Posted: Thu Oct 19, 2006 6:25 am
by Ollie Saunders
require_once and include_once are slow, replace them with require and include. You can write a wrapping function to emulate the onceness:
Code: Select all
function loadFile($file)
{
static $loads = array();
if (!$loads[$file]) require $file;
$loads[$file] = true;
}
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
// Main.php
loadFile('Foo.php');
var_dump($foo); // NULL!!
loadFile('Bar.php');
var_dump($bar); // 10
Posted: Thu Oct 19, 2006 6:33 am
by Mordred
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:
Hmm, I doubt that the _once() functions do something much different than your code, can you support your claim somehow? Profiling data?
Edit:
In fact, I'm sure it doesn't:
http://lxr.php.net/source/pecl/apc/apc_zend.c#142
Posted: Thu Oct 19, 2006 6:42 am
by Ollie Saunders
No, sorry I can't. I only heard it from someone else. I think you will find it in a couple of those talks.