Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP performance

What can I do to increase the performance/speed of my PHP scripts without installing software on my servers?

like image 649
UnkwnTech Avatar asked Aug 15 '08 18:08

UnkwnTech


People also ask

Is PHP performance good?

PHP's performance is fine. Unless you're designing 3d games, of course. The differences are both negligible and flame-bait.

Why is PHP slow in performance?

So why is PHP slow in comparison with some other languages? PHP is a dynamic, interpreted language. This means that it is not compiled to machine language but rather read at runtime. PHP also has a share-nothing architecture, so on every request it interprets everything from fresh.

Why is PHP so fast?

PHP codes runs much faster than ASP because it runs in its own memory space while ASP uses an overhead server and a COM based architecture. Less Expensive Software – In working with PHP, most tools associated with the program are open source software, such as WordPress, so you need not pay for them.


2 Answers

Profile. Profile. Profile. I'm not sure if there is anything out there for PHP, but it should be simple to write a little tool to insert profiling information in your code. You will want to profile function times and SQL query times.

So where you have a function:

function foo($stuff) {
    ...
    return ...;
}

I would change it to:

function foo($stuff) {
    trace_push_fn('foo');
    ...
    trace_pop_fn('foo');
    return ...;
}

(This is one of those cases where multiple returns in a function become a hinderance.)

And SQL:

function bar($stuff) {
    trace_push_fn('bar');

    $query = ...;
    trace_push_sql($query);
    mysql_query($query);
    trace_pop_sql($query);

    trace_pop_fn('bar');
    return ...;
}

In the end, you can generate a full trace of the program execution and use all sorts of techniques to identify your bottlenecks.

like image 144
Frank Krueger Avatar answered Sep 20 '22 13:09

Frank Krueger


One reasonable technique that can easily be pulled off the shelf is caching. A vast amount of time tends to go into generating resources for clients that are common between requests (and even across clients); eliminating this runtime work can lead to dramatic speed increases. You can dump the generated resource (or resource fragment) into a file outside the web tree, and then read it back in when needed. Obviously, some profiling will be needed to ensure this is actually faster than regeneration - forcing the web server back to disk regularly can be detrimental, so the resource really does need to have heavy reuse.

You might also be surprised how much time is spent inside badly written database queries; time common generated queries and see if they can be rewritten. The amount of time spent executing actual PHP code is generally pretty limited, unless you're using some sub-optimal algorithms.

Neither of these are limited to PHP, though some of the PHP "magicy" approaches/functions can over-protect one from thinking about these concerns. For example, I recently updated a script that was using array_search to use a binary search over a sorted array, and gained the expected exponential speedup.

like image 41
Adam Wright Avatar answered Sep 21 '22 13:09

Adam Wright