0

I have a php-system on PHP 7 which loads about 40 classes to work. It needs about 180ms to display a webpage, so I was starting to have a look if I could save some time by optimizing the code. I couldn't use a cache because the data shown on the page depends on which user is logged on and is highly individual.

I did some replacements, like using require instead of require_once and I replaced functions in loops like

for($i = 0; $i < $this->GetItemCount(); $i++)

with:

$len = $this->GetItemCount();
for($i = 0; $i < $nLen; $i++)

but - to my suprise - this only gets me a 4-5ms better result.

I then just made some experiments with microtime() to see where the time is consumed. It seems that just including and parsing all the classes takes about 120ms - and the work which is done then only consumes about 60ms.

I thought that including might be the problem so I put all the code in one big file - but that didn't help very much.

So it seems that the parsing/compiling is still very slow even with PHP 7. I had a look which possibilities there are to pre-compile the code to get better results, but it seems that solutions like APC or OPCache is only working when you have access to the server. I mostly use shared-hostings, so the question is: Are there any possibility to "compile" the php to bytecode which could be processed faster? I understand that it's tricky in shared hosting because the compiled code couldn't be stored in memory, but I don't understand why there doesn't seem to be a compiler which compiles the php-file to a byte-code file which could be read in less time then doing all the parsing again and again.

Werner
  • 1,695
  • 3
  • 21
  • 42
  • 1
    OPcache is built-in since PHP5.5. The question is, do you need to load all those 40 classes all the time? If not, you might want to look into autoloading. – Charlotte Dunois Aug 21 '16 at 13:23
  • Thanks for your reply. Is it possible to use OPCache with PHP in CGI-Mode? Because most hoster use that. Autoloading might be an option, I didn't try it yet because what I read most people say it's very slow. But I'll give it a try. – Werner Aug 21 '16 at 14:22
  • Possible duplicate of [Supersimple static file based (html) php site cache](http://stackoverflow.com/questions/8758097/supersimple-static-file-based-html-php-site-cache) – Paul Sweatte Aug 31 '16 at 18:56

0 Answers0