Locating Bottlenecks
There are several techniques you can use when working with PHP applications to improve performance. These are probably some of the most fundamental ones:
Those tasks, specially caching, will greatly increase the performance of your application. But once you have followed those advises, it is time to have a closer look to the code of your application.
Bottlenecks
Bottlenecks are those parts of an application whose execution takes more time that it needs to.
Once you have a working application, (almost) bug-free, you focus on improving its performance and responsiveness. But in almost any project, reviewing the whole code in the search of room for improvement is just unacceptable.
What to do then? Well, then you have to focus on the parts of the code that take the most part of the execution time. That is what the Profiler is for. The Profiler watches the execution of your application, and generates a detailed performance analysis you can use to find out which parts of the code are subject to be reviewed, since improving them would have a reasonable impact on the overall performance of the application.
Performance Analysis
To generate a performance analysis with RadPHP, follow these steps:
You will then get a window with a treeview that lets you browse all the functions called during the execution of your application, and tells you how much time (in milliseconds or percentage of the overall execution time) each one of them took.
Now you can locate the parts of your application that are slowing you down, and get into the code and try to improve it.
Improve your Code
Improving the speed of your code might not be easy, though, and sometimes it might be just impossible.
First, you should try to think about alternative algorithms to those you are using, that might improve the performance of the operation. During development, a working code is often more important than a clean, efficient code, specially when there are schedules to be met.
If you are not sure, between two or more algorithms, of which one is the fastest, test them against the Profiler, and you will get your answer.
Of course, knowing how to properly use PHP to gain speed during execution is important too. For example, here you are a couple of tips you might want to follow:
Lets Check
Time now to put in practice the Profiler. Lets, for example, check if one of these tips does actually work. Here is the code we will use for that:
We define a function that prints a simple string containing a variable a thousand times. Then, we create a sibling for that function that does exactly the same, but separating the variable and the string, joining them with a dot. This is what the Profiler tell us:
[caption id="attachment_139" align="aligncenter" width="450" caption="Profiler in action."]
[/caption]
The second way, $i."<br />", is about 33% faster than the former, "$i<br />".
Lets check now the comparison operators:
Results:
Check your Own Code
Now is time for you to check your code, and try to improve its performance. These popular PHP benchmarks should give you a hint of minor changes that could mean a great deal for the performance of your application, and use the Profiler to check these or other PHP syntax choices, as well as more complex algorithms.
- Upgrade PHP. Newest PHP versions will include lots of changes, not just additional features, but also performance improvements.
- Use cache for database results and rendered pages.
- Disable PHP extensions you are not using when you deploy your application to a web server.
Those tasks, specially caching, will greatly increase the performance of your application. But once you have followed those advises, it is time to have a closer look to the code of your application.
Bottlenecks
Bottlenecks are those parts of an application whose execution takes more time that it needs to.
Once you have a working application, (almost) bug-free, you focus on improving its performance and responsiveness. But in almost any project, reviewing the whole code in the search of room for improvement is just unacceptable.
What to do then? Well, then you have to focus on the parts of the code that take the most part of the execution time. That is what the Profiler is for. The Profiler watches the execution of your application, and generates a detailed performance analysis you can use to find out which parts of the code are subject to be reviewed, since improving them would have a reasonable impact on the overall performance of the application.
Performance Analysis
To generate a performance analysis with RadPHP, follow these steps:
- Open your PHP application on RadPHP.
- Go to Run > Enable profiler.
- Execute your application (Run > Run).
- Go to Run > Load Last Profiler Results.
You will then get a window with a treeview that lets you browse all the functions called during the execution of your application, and tells you how much time (in milliseconds or percentage of the overall execution time) each one of them took.
Now you can locate the parts of your application that are slowing you down, and get into the code and try to improve it.
Improve your Code
Improving the speed of your code might not be easy, though, and sometimes it might be just impossible.
First, you should try to think about alternative algorithms to those you are using, that might improve the performance of the operation. During development, a working code is often more important than a clean, efficient code, specially when there are schedules to be met.
If you are not sure, between two or more algorithms, of which one is the fastest, test them against the Profiler, and you will get your answer.
Of course, knowing how to properly use PHP to gain speed during execution is important too. For example, here you are a couple of tips you might want to follow:
- Be original, do not copy. Avoid copying variables into other variables when it is not necessary.
- Do not use variables inside strings. Strings are parsed faster that way. If you need to use a variable on a string, just join the string and the variable instead of using the variable directly on the string.
- Use the identical operator, ===. Apart from being stricter than ==, which is usually what you need, === is way faster.
Lets Check
Time now to put in practice the Profiler. Lets, for example, check if one of these tips does actually work. Here is the code we will use for that:
function test1()
{
for($i = 0; $i < 10000; $i++)
{
echo "$i<br />";
}
}
function test2()
{
for($i = 0; $i < 10000; $i++)
{
echo $i."<br />";
}
}
test1(); // Test with a variable inside the string.
test2(); // Test with string a variable joined instead.
We define a function that prints a simple string containing a variable a thousand times. Then, we create a sibling for that function that does exactly the same, but separating the variable and the string, joining them with a dot. This is what the Profiler tell us:
[caption id="attachment_139" align="aligncenter" width="450" caption="Profiler in action."]

The second way, $i."<br />", is about 33% faster than the former, "$i<br />".
Lets check now the comparison operators:
function test1()
{
for($i = 0; $i < 10000; $i++)
{
if($i == 5000)
print "Got to the middle!";
}
}
function test2()
{
for($i = 0; $i < 10000; $i++)
{
if($i === 5000)
print "Got to the middle!";
}
}
test1();
test2();
Results:
Check your Own Code
Now is time for you to check your code, and try to improve its performance. These popular PHP benchmarks should give you a hint of minor changes that could mean a great deal for the performance of your application, and use the Profiler to check these or other PHP syntax choices, as well as more complex algorithms.

Comments
-
Please login first in order for you to submit comments