How to Improve Your Business ROI with Java Application Log Monitoring

Website Performance: PHP

The World-Wide Web offers more PHP performance tips than can be comfortably discussed in one article, so the following is merely a list that can be used for reference purposes. The tips are divided into categories to group similar things together and make it easier to find what we need. The author’s search for tips was extensive, but completeness still cannot be guaranteed.These tips fall into category #3.1 (the server executes a script) in the taxonomy of tipsthat we have been working with.Because of the sheer volume, these tips have not been tested. The reader is expected to test them in his own production environment before relying on them. This is not unreasonable, though, because the value of most tips depends on the unique environment within which PHP operates.

Finding the Bottleneck

Considering these tips to be “best practices” and using them on an ongoing basis is a good idea. However, when faced with a performance problem, the first step should always be to find the cause of the problem rather than to run through a list of tips. Understanding the cause, identifying options, selecting an option, implementing a fix, and measuring the result all come later. Finding the bottleneck is step one. Here are some tips that will help us with that all-important first step:

Use monitor.us or Monitisto measure, benchmark, and monitor. Networks, especially the Internet, continuously change. What performs well now may be a bottleneck in 5 minutes.

Profile the code. Know which parts of the code take the most time and focus attention on those sections.

Use realistic tests with realistic data under realistic loads. Use the production server if possible.

Caching

Some authors identify caching as one of the most effective ways to improve performance. Here are some tips to help us out:

Use opcode caching so the scripts are not recompiled every time they are accessed. Example: Enable the Windows Cache Extension on Windows platforms. It caches opcode, files, relative paths, session data, and user data.

Consider distributed caching in a multi-server environment.

Call imap_headers() before calling imap_header().

Compiling vs. Interpreting

Compiling translates PHP source code into machine code. Interpreting performs the same translation, but line-by-line as the code executes. Translating into opcode is somewhere in-between; it translates source code into opcode, then translates the opcode into machine code afterward. Here are some tips regarding compiling and interpreting:

Compile PHP into machine code before it goes live. Caching opcode is not as good, but is still better than interpreting. Compiling PHP code into a C extension is one option.

Content Reduction

Smaller is often faster. Here are some tips to help us reduce the size of our code:

Provide less functionality per page.

Unclutter web pages.

Strip comments and extraneous white space at build time if the code is being interpreted.

Make fewer database queries.

Multithreading & Multiprocessing

In order from fastest to slowest:

multithreading (within a single process)

multiprocessing (e.g., pcntl_fork, cron jobs)

serial processing (the usual “one line followed by another” approach)

Mutlithreading is not available in PHP, but a mutli-threaded C program can be used as a PHP extension. There are several other ways to use or simulate multiprocessing, but none is as well supported and some may be slower than serial processing.

Strings

String processing seems to be one of the most frequently executed operations in most programming languages. Here are some tips to help us make our string operations even faster:

PHP’s concatenation operator (the dot operator) is the fastest form of concatenation in most cases.

Instead of concatenating strings in a print, separate them with commas in an echo.

Use str* functions instead of functions that use regular expressions. pos() is faster than preg_match(), which is faster than ereg(). str_replace() is faster than preg_replace(), which is faster than ereg_replace().

Some people say that surrounding strings with single quotes performs better than with double quotes. Others say it makes no difference. Of course, if you want to include variables inside the string, single quotes won’t work.

If you need to know whether a string is shorter than some value (say 5), use isset($s[4]) instead of strlen($s) < 5. Flip the conditions if you want to know if it’s longer.

To concatenate many small strings into one large string, try creating an output buffer with ob_start, then use echo to append into the buffer. When done, use ob_get_contents to retrieve the string.

Regular Expressions

Regular expressions give us all kinds of flexibility when comparing or searching strings, but their performance cost is usually non-trivial.

Use str* functions instead of regular expressions whenever possible.

Use [aeiou]rather than (a|e|i|o|u).

The simplest regular expression is usually the fastest.

Unset PCRE_DOTALL if appropriate.

Use ^.* instead of .*.

Simplify. Example: Use a* instead of (a+)*.

Iteration Constructs (for, while)

Iteration (repetition, looping) is a fundamental structured programming construct. It’s hard to imagine a non-trivial application that doesn’t use it. Here are some tips to help us improve the performance of our iteration constructs:

Move as much as possible outside the loop (function calls, SQL queries, etc.).

Use i=maxval; while (i–) instead of for (i=0; i<maxval; i++)to eliminate one operation. It’s even more important if maxval is a function call.

Use foreach for iterating through collections and arrays.

Selection Constructs (if, switch)

Like iteration, selection is also a fundamental structured programming construct. Here are some tips to help us improve its performance:

Switches and else-ifs should list the most-often-true conditions ahead of the less-often-true conditions.

Some say if-else is faster than switch/case. Others say the opposite.

Use elseif instead of else if.

Functions & Parameters

Chopping code up into functions can eliminate redundancy and make our code more readable, but at what cost? Here are some tips to help us optimize our use of functions:

Pass objects and arrays in and out by reference, not by value.

Inline functions if they are used only in one place. Consider inlining them if they are invoked in multiple places, but beware the maintainability issue.

Know the complexity of the functions you use. Example: similar_text()is O(N**3), which means that doubling the length of the string multiplies the time requirement by 8.

Do not use return-by-reference to increase performance. The engine automatically optimizes this on its own.

No one likes to be interrupted in the middle of playing their favorite online game with the message “our servers are experiencing issues, please check back later.” But (read more)

01.07.2016

Last week’s DevOpsDays Silicon Valley conference was a two-day thrill with a whirlwind of inspiring presentations and super-smart devops talks! The conference took place on June 24-25, 2016 (read more)

There’s never been a more exciting time to be involved in the business of technology. It seems that every day now we hear about some exciting new breakthrough (read more)

01.07.2016

Last week’s DevOpsDays Silicon Valley conference was a two-day thrill with a whirlwind of inspiring presentations and super-smart devops talks! The conference took place on June 24-25, 2016 (read more)