How can I speed up 1800 line PHP? This slows my pageload to 10 seconds / view

I designed my code to put all the important functions in a single PHP file, whose length is 1800 lines.

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

I find that it takes about 10 seconds to load all these functions, although I have nothing more than a few global arrays and many other functions. For example, the main AJAX processor code takes 8 seconds to perform a simple syntax check (whose operational function is stored in codeBank.php).

When I comment on require_once, the AJAX response time increases from 10 s to 40 ms, so it’s pretty clear that PHP is trying to do something with these 1800 lines of functions. This is even when installing APC, which is surprising.

What should I do to return the speed of my code to a level below 100 ms? Can't I get an advantage in the cache? Do I need to trim this file with one system file into different parts? Are there other subtle things that I could do to spoil my answer time?

Or to forbid all this, what are some tools which need to be dig further, what PHP operations hit on speed?

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

[EDIT] Solved.

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

As many of you kind people have pointed out, there is no logical reason why simply having a php 1800 function library should cause a slowdown. What actually happens is that I had a debug string that called one of the longer API call functions. Whenever I included a PHP file, I built a whole data structure from the deleted, requested data.

As soon as I killed this line, everything returned to the fast 30 ms responses. It remains strange to me that require_once () opens a php file every time an AJAX script is called. But, that I am not in shape and forget that every time the AJAX script is finished, it closes and reopens and recompiles every time.

+4
source share
6 answers

You may have a file with 100,000 lines of code, and it still should not take 10 seconds.

There is probably an initialization code that you do not understand that works. Take a look at the profiler (xdebug or the one in Zend Studio), and find out what exactly slows down before you go along the path of optimization. If you consider this simply because the file is 1800 lines long, you are mistaken.

+6
source

make sure that there is no sleep () function :) also use apc to cache the file after processing it, if it still slows down when apc caches the file, then the problem lies elsewhere. I included files with 20,000 lines without any problems.

+1
source

Can you combine functions into smaller files and download on demand?

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

If so, you can use PHP autoload. __autoload() or for a more flexible solution, check spl_autoload_register() .

0
source

Definitely splitting this file no matter - if nothing else, then the performance you get from having your code base more organized will be worth it.

0
source

It is very surprising that inclusion (even 1800 lines) can significantly slow it down. First, you might want to look at the code to make sure that it is not performing unnecessary processing. In fact, parsing a file does not have to be such a bottleneck.

Thus, it includes one of the key bottlenecks in many PHP programs. You might consider reorganizing the code into either a few smaller files (of which you will include only what you need), or (possibly much simpler), use the PHP __autoload function. This is a function that is automatically called whenever you reference a class that does not exist, so you need to put your functions in 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); 
0
source

I can’t imagine that the 1800 line file is a problem, I only have one of the many classes that I include in all my pages, which are 1870 lines, and this never causes a problem. Is there a file or network access in the file that you include?

0
source

Source: https://habr.com/ru/post/1308839/


All Articles