Speeding up require_once()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
amir
Forum Contributor
Posts: 287
Joined: Sat Oct 07, 2006 4:28 pm

Speeding up require_once()

Post 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
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Optimize the code (more)? Break it into smaller chunks? Preparse them? Its difficult to say without seeing them.
User avatar
thomas777neo
Forum Contributor
Posts: 214
Joined: Mon Mar 10, 2003 6:12 am
Location: Johannesburg,South Africa

Post 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?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Make sure the file is required/included once, and only once.. And use require/include instead of the *_once version...
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Can the called scripts be smallerized?
amir
Forum Contributor
Posts: 287
Joined: Sat Oct 07, 2006 4:28 pm

Post by amir »

i need speed up this process
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

yup... time to dive into design patterns, my friend.
amir
Forum Contributor
Posts: 287
Joined: Sat Oct 07, 2006 4:28 pm

Post by amir »

Now i have got an idea atleast,
Thanks all of you!
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post 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?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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

Code: Select all

// Foo.php
$foo = 10;

Code: Select all

// Bar.php
$GLOBALS['bar'] = 10;
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post 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
Last edited by Mordred on Thu Oct 19, 2006 6:44 am, edited 1 time in total.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
Post Reply