Is your PHP site slow? Time to look under the hood
Have you ever waited for a page to load longer than it takes to brew your morning coffee? You know that feeling when a client or user writes: "The site is running slow." Most often, the problem isn't "bad" PHP, but how we use its capabilities. PHP is a powerful and flexible language, but without proper configuration and profiling, it can turn into a "turtle."
In this article, we won't give boring lectures. We'll take a real project, break it down to its atoms using profilers, and show you how to turn slow code into lightning-fast code. You'll learn what PHP performance is, how to measure it, and most importantly, how to improve it. Let's go!
Why can PHP be slow? Myths and reality
Many developers believe that PHP is inherently slow. This is a myth. Modern versions (7.4+, 8.x) show excellent results. The problem is most often the following:
- Unoptimized loops and database queries — a classic issue.
- Memory leaks — the script consumes more and more resources.
- Lack of caching — recalculating the same thing every time.
- Too many includes and autoloaders — the file system suffers.
But before optimizing blindly, you need to understand: where exactly is the bottleneck. This is where profiling comes in.
What is PHP profiling and why is it needed?
Profiling is the process of collecting data about a script's execution: the execution time of each function, the number of calls, memory consumption. It's like an X-ray for your code. You immediately see which method is "eating" the most resources.
Without profiling, you risk optimizing something that already works fine, while the real problem goes unnoticed. Therefore, PHP profiling is the first step to speeding things up.
Profiling tools: from simple to complex
There are several ways to look inside a PHP script. We'll look at three of the most popular ones.
1. Xdebug — the grandfather of profilers
Xdebug can not only debug but also profile. It creates cachegrind files that can be analyzed in special programs (e.g., KCachegrind or QCachegrind).
Installation:
pecl install xdebugConfiguration in php.ini:
xdebug.mode = profilexdebug.output_dir = /tmp/profilingAfter running the script, a file will appear in the /tmp/profiling folder. Open it in KCachegrind and you'll see a "call tree" with execution times. For example, you might find that the getUserData() function takes 80% of the time.
2. Blackfire.io — modern and convenient
Blackfire.io is a profiler from SensioLabs (the creators of Symfony). It works as a PHP extension and collects data in real-time. Plus, it has a beautiful web interface and automatic recommendations.
Installation:
curl -s https://blackfire.io/install.sh | bashThen connect to the API and start profiling:
blackfire run php script.phpBlackfire will show not only time, but also the number of I/O operations, memory allocation, and even SQL queries.
3. Built-in functions: microtime and memory_get_usage
If you need to quickly check a specific piece of code without installing heavy tools, use simple functions:
$start = microtime(true);// your code$end = microtime(true);echo "Execution time: " . ($end - $start) . " sec";For memory measurement:
echo memory_get_usage() / 1024 . " KB";This won't give you the full picture, but it will help find obvious issues.
How to analyze profiling results?
Let's say you have a profile. What should you look for?
Blogs
Book Recommendations
Read also
Type converting slices of interfaces in Go: a complete breakdown with examples
Generic or Associated Type in Rust: What to Choose in 2025?
Kotlin in 2025: What's New? A Complete Guide for Beginners
PHP in the World of Microcontrollers: An Unexpected Duo for Embedded
Go Language: How a Modest Google Project Became a Programming Star