开发者

How can I speed up a 1800-line PHP include? It's slowing my pageload down to 10sec/view

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开发者_如何学Python it closes and is getting reopened and recompiled each time.


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.


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.


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().


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.


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);


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜