PHP Classes

Class to optimize the PHP file: I need to optimize my already coded php file

Recommend this page to a friend!
  All requests RSS feed  >  Class to optimize the PHP file  >  Request new recommendation  >  A request is featured when there is no good recommended package on the site when it is posted. Featured requests  >  No recommendations No recommendations  

Class to optimize the PHP file

A request is featured when there is no good recommended package on the site when it is posted. Edit

Picture of nilesh by nilesh - 6 years ago (2016-05-18)

I need to optimize my already coded php file

This request is clear and relevant.
This request is not clear or is not relevant.

+2

I need the file fully optimized so that all redundant code should be placed under a different function, all unnecessary code (unused class member variable, unused local variables) should be removed, etc..

In all, it should give me the PHP file which could be executed so fast, considering all areas, aspects of optimization.

  • 1 Clarification request
  • 1. Picture of Gary Bhat by Gary Bhat - 6 years ago (2016-05-19) Reply

    There might not be one I have seen but you can try using blackfire.io

    • 2. Picture of Christian Vigh by Christian Vigh - 6 years ago (2016-05-19) in reply to comment 1 by Gary Bhat Comment

      There is no magical solution. Optimization is highly dependent on the running context and current implementation and, usually, removing unused variables will make your code cleaner but won't affect its execution time (well, you will save a few memory and a few nanoseconds).

      If your code runs slow, then you should first identify the faulty part and extract it to make a standalone benchmark. Tools like the xdebug profiler (xdebug.org/docs/profiler) can help you identify the 20% of functions that take 80% of the execution time. It is often the opportunity to rework your algorithms in a better way, for example by removing invariants from loops, or redesigning your sql queries (that may lead to redesigning indexes or relationships in your database).

      If the problem comes from a page being so much accessed that it makes your cpu crazy, then you could consider php extensions such as opcache (et al) that reads your php code, compiles it and put it in the cache, so that the next time your page is requested, the compiled source code is already present in the cache and will be used as is, thus avoiding an extra compilation phase.

      Of course, you can also remove comments and white spaces from your source files, using the php_strip_whitespace() function ; you could also put the results on a RAM disk for faster access. You will reduce a little bit disk io and compilation time but don't even expect to improve the execution time by more than a few tenths of percents, even if your source code is lengthy.

      As a conclusion, when optimizing code (either in php or in other languages), you need to understand how your runtime environment works under the hoods, which parts of your code take too much time, make a benchmark of the extracted code, optimize it, reintegrate it into your runtime environment, and test it again. This is an iterative process that can be tedious and sometimes will lead you to no solution at all (except by totally redesigning your implementation), but it's worth the understanding of what goes wrong.

      I have developed a class loader that puts source files onto a ramdisk after preprocessing them with the php_strip_comments() function. This class is not yet published on this website since I consider there are still too many specific parts, but if you feel it could be of interest to you (at least as an example), I can publish it. Note however that I did not notice dramatic performance improvements using such a technique...

    • 3. Picture of Manuel Lemos by Manuel Lemos - 6 years ago (2016-05-23) in reply to comment 2 by Christian Vigh Comment

      I agree with you Christian. You can do many things to optimize your code but you need to understand first what is your bottleneck.

      Sometimes the bottleneck is in the database accesses. In that case you can do things like using the right database indexes or use a caching class to avoid accessing the database multiple times to get the same information.

      As for your new class to load source files from RAM, that sounds interesting. I wonder how it works because it looks innovative. Please submit that class so we can take a look.

    • 4. Picture of Christian Vigh by Christian Vigh - 6 years ago (2016-05-23) in reply to comment 3 by Manuel Lemos Comment

      Well, there is no magic in it : whenever a missing class is requested, the class loader takes the source file in its original location and copies it to a destination, which can be a fast IO disk such as a ramdisk.

      The copy is made only once, unless the original source file has been modified since.

      During the process, comments and whitespaces are stripped ; an option can also compress the destination file, which will be uncompressed whenever requested (in some cases, the overhead implied by decompressing a file is less that the overhead of disk io required for reading the uncompressed version of the file).

      It also allows your classes to implement funny things such as a __static_construct() method.

      If you still feel that it could be of interest for phpclasses users, please tell me, I will package it to work as a standalone class (ie, outside my Framework).

    • 5. Picture of Manuel Lemos by Manuel Lemos - 6 years ago (2016-05-23) in reply to comment 4 by Christian Vigh Comment

      Sure sounds very interesting as many of your classes. I need to take a look at your ram disk implementation.

    • 6. Picture of Christian Vigh by Christian Vigh - 6 years ago (2016-05-23) in reply to comment 5 by Manuel Lemos Comment

      oh I'm sorry for the mistake : there is absolutely no ramdisk implementation. My formulation was wrong : the autoloader takes source files, applies some transformations such as stripping comments or compressing them, and put the results into a dedicated directory, if the destination file is missing or older than the original file.

      I just suggested that for faster access to those dedicated files, you could tell the autoloader class to put them in a fast IO file system, such as a ramdisk.

    • 7. Picture of Manuel Lemos by Manuel Lemos - 6 years ago (2016-05-24) in reply to comment 6 by Christian Vigh Comment

      Oh, I see. Anyway if you have that class already it is always useful to submit.

Ask clarification

Recommend package
: 
: 
For more information send a message to info at phpclasses dot org.