views:

216

answers:

6

I designed my code to put all important functions in a single PHP file that's now 1800 lines long.

I call it in other PHP files--AJAX processors, for example--with a simple "require_once("codeBank.php")".

I'm discovering that it takes about 10 seconds to load up all those functions, even though I have nothing more than a few global arrays and a bunch of other functions involved. The main AJAX processor code, for example, is taking 8 seconds just to do a simple syntax verification (whose operational function is stored in codeBank.php).

When I comment out the require_once, my AJAX response time speeds up from 10sec to 40ms, so it's pretty clear that PHP's trying to do something with those 1800 lines of functions. That's even with APC installed, which is surprising.

What should I do to get my code speed back to the sub-100ms level? Am I failing to get the cache's benefit somehow? Do I need to cut that single function bank file into different pieces? Are there other subtle things that I could be doing to screw up my response time?

Or barring all that, what are some tools to dig further into which PHP operations are hitting speed bumps?

==========================

[EDIT] Resolved.

==========================

As many of you kind people have noted, there's no logical reason why just having a 1800 line php function library should cause slowdowns. What was actually happening is that I had a debug line that was invoking one of the longer, API-calling functions. Whenever I was including the PHP file, I was constructing a whole data structure from remote, queried data.

Once I killed that line, everything went back to snappy 30ms responses. What remains odd to me is that require_once() opens the php file every time the AJAX script was getting called. But that's me being out of shape and forgetting that every time the AJAX script is finished it closes and is getting reopened and recompiled each time.

A: 

Can you group the functions into smaller files and load on demand?

Are they global functions or classes, maybe with static methods?

If so, you could benefit from PHP's autoloading. Look into __autoload() or for a more flexible solution, look at spl_autoload_register().

alex
A: 

Definitely split that file up regardless - if nothing else the productivity gains you'll get from having your codebase more organized will be worth it.

Amber
you will not gain productivity, if you'll use all/most of the classes grouping classes into one file is actually faster than the I/O time required to fetch and parse multiple files.
@beagleguy You will gain your productivity though - the less headaches of wading through a 1800 line file.
alex
he's trying to gain performance :)
@beagleguy - the productivity Dav is talking about is programmer productivity. It'll be much easier to find and maintain functions if they are split into logical, manageable files.
nickf
1800 lines is not really all that large of a file.
Michael
@Michael with something like C, yes... but we're talking about PHP here...
Aviral Dasgupta
@aviraldg check out the source for Cake or CodeIgniter, it's easy to find many files with more than 1800 lines.
Michael
@Michael Django.
Aviral Dasgupta
Honestly, the number of lines is not what prompted my comment - rather, the fact that one has a file which is just a smörgåsbord of functions, many of which won't be used in any given instance of inclusion and most likely aren't related to one another, is an organization nightmare.
Amber
In fact, breaking the file down into smaller, more topical files would have a convenient side effect of *making it easier to figure out what's causing the slowdown at load* - because the I/O parse time is definitely not what's causing 10 seconds of delay in running the scripts here.
Amber
Actually, the file is 1800 lines long because they're all functions, organized and commented well. Since they're all functions, it's equivalent to a library that does no execution code on its own. I found the problem, and it's kind of embarrassing. More in the main thread.
somerandomguy
Libraries are generally designed for a coherent purpose, though, rather than just necessarily sticking every single function in them. Perhaps I got the wrong impression, but it did not seem like these functions were limited to a focused purpose.
Amber
+5  A: 

You could have a file with 100 000 lines of code and it still shouldn't take 10 seconds to load.

There's probably some initialization code in there that you don't realize is running. Take a look at a profiler (xdebug or the one in Zend Studio) and find out exactly what's causing a slow down before you go down the route of optimization. If you think it's simply because the file is 1800 lines long, you're on the wrong track.

Koobz
Kudos to you, good sir! We ended up solving it with a quick eyeballing the next day when we were fresh.
somerandomguy
A: 

It is very surprising that an include (even of 1800 lines) could slow it down that much. Firstly, perhaps you should look over the code to make sure it's not performing unnecessary processing. Actually parsing the file shouldn't be such a bottleneck.

That said, includes are one of the key bottlenecks in a lot of PHP programs. You could consider refactoring your code either into a number of smaller files (of which you would only include what you need), or (probably much easier), use PHP's __autoload function. It is a function which is automatically called whenever you refer to a class which doesn't exist, therefore you'd need to put your functions into a static class. eg:

// include/ajaxlib.php
class AjaxLib {
    public static function renderResponse($data) {
        // whatever
    }
}

// a common include file:
function __autoload($class) {
    require 'include/' . strtolower($class) . '.php';
}

// your code:
AjaxLib::renderResponse($foo);
nickf
+1  A: 

make sure there isn't a sleep() function in there :) also use apc to cache the file after it's been processed, if it's still slow when apc is caching the file then the problem lies elsewhere. I've included files with 20,000 lines with no problems.

A: 

I can't imagine an 1800 line file being a problem, I have just one of the many classes that I include in all of my pages that is 1870 lines, it never causes a problem. Is there any file or network access in the file you are including?

Michael