To identify and correct them, use web server logs or tools like XDebug. As more errors are eliminated and the application grows stable, reducing the page load time becomes the next focus.
Now, the major source of performance issues lies in the core business logic and integration of various components like databases, cache, and third-party APIs. Benchmarking your applications and identifying the processing time taken by different components becomes pivotal.
As the application matures, performance can be further improved by identifying frequently used blocks of code, minimizing redundant calls, caching, and distributing workloads across multiple servers. At this stage, continuous monitoring of errors, logs, and key metrics like response time and throughput is necessary to maintain the high availability of your applications and meet your SLAs.
As the language evolved over the years, many performance bottlenecks were identified and incorporated as part of the language itself. Here we list some of the frequently encountered performance issues in PHP code. Let's take a simple script that loops through an array of objects. We can achieve this via one of three possible loop constructs.
Here we use the for and foreach loops to understand how small changes can affect the speed of execution. For demonstration, we ran the snippet by initializing the array with thousand objects and got the following results:.
As we can see, eliminating repeated function calls improves the performance of the loop. Since every line of code will be evaluated in PHP, pre-calculating repeated values can boost performance. Caching is a well-known technique to improve the performance of any web application and is one of the most commonly offered pieces of advice given by any PHP developer.
PHP versions are fully supported for two years from initial release. Below are the supported versions of PHP. There will be compatibility issues when migrating between these two versions, but the advantages, especially with the performance gain, will outweigh the development cost and time for the modifications. If you are using the following versions below, I suggest you upgrade to a current version for better PHP performance. PHP Versions. This may seem the last thing developers should care about, but there has been a lot of tests conducted to prove using single quotes, especially in larger loops and strings, is significantly faster than using double quotes.
When you think about PHP performance optimization, the usage of single quotes for strings matters. From this test, the string with single quotes runs more than twice as fast compared to the string test with double quotes. The difference in milliseconds might look negligible, but this performance gain would help PHP web applications that are being accessed by hundreds of users every minute. Loops are used mainly to traverse an array; but if the condition of the loop uses the count function to count the number of array elements, then there will be an overhead cost in using this function.
The best way to traverse an array using loops is to store the number of elements in an array once, and then use that variable for the loop condition. Because if the count function is used in a for loop or loop, then every time the loop iterates, the program recounts the array which adds to the number of processes in each iteration. When querying the database, a connection has to be made and one way to do that is to declare a connection variable. We all know that each variable used or declared uses memory, so it would be a good practice to close the connection after the query or all the queries are complete.
Similar to opening files, after reading or writing to the file, the variable that handles the connection must be closed. Closing connections will greatly save memory usage even though multiple people are accessing the same request of a web application.
Unlike public methods or properties that need to have its class instantiated before it can be accessed, static methods can be directly called. But you do need to be selective about it. This code has survived in the wild for over five years, and I think I will still be using it in another five. This code is also a good candidate because it does not access external resources, so there is only one component to examine. I ran this after every change to the code, so that I could be sure that the changes were not affecting the output.
Code profiling allows you see how each part of your program is contributing to its overall run-time. This helps you to target your optimization efforts. Unfortunately, the built-in profiling tool in Eclipse seems to only support the Zend debugger, so I have to profile my scripts on the command-line. This produces a profile file, which you can use any valgrind-compatible tools to inspect. I used kcachegrind. Before profiling, I had guessed that the best way to speed up the code would be to reduce the amount of string concatenation.
I have lots of tight loops which append characters one-at-a-time:. Xdebug showed me that my guess was wrong, and I would have wasted a lot of time if I tried to remove string concatenation. This is the simplest way to add UTF-8 support when iterating strings, is commonly suggested , and is a bad idea.
If you have an ASCII string in PHP and want to iterate over each byte, the idiomatic way to do it is with a for loop which indexes into the string, something like this:. The idiomatic PHP for iterating over a multi-byte string one character at a time would be:. Split your strings into bytes or characters before you iterate, and write methods which operate on arrays rather than strings. I learned about this trick from StackOverflow , but it might not be the fastest way.
Save my name, email, and website in this browser for the next time I comment. Notify me of followup comments via e-mail. All rights reserved Terms of Service.
Issac on April 1, If you are a developer, it is essential for you to optimize your script early in the development process itself. Calculate Only Once Calculate and assign the value to the variable if that value is getting used numerous time rather than calculating it again and again where it is being used.
Haruni April 2, , am. Thank you! It helped a lots, keep doing great job. Tariq Aziz April 2, , am. Mukesh April 2, , am. Very helpful tips! Thanks for the article. Rod Elias April 2, , pm. Thanks for such valuable tips. Please keep posting more stuff about PHP. Pratik April 7, , am. Dustin April 7, , am. Dave April 9, , am. Robin April 19, , am. Mike Dinescu May 2, , am. DO use a profiler!
0コメント