PHP Code Profiling: Finding Bottlenecks and Speeding Up Your Site

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why is your PHP code slow?

Every developer has encountered a situation where a PHP site starts to slow down. Pages load in 5-10 seconds, the database "crashes," and the server struggles under load. Most often, the problem lies not in the hardware, but in the code — in so-called "bottlenecks."

Profiling is the process of collecting data on how much time and memory each part of your script consumes. Without profiling, you are working blindly, guessing what exactly is slowing down the application. In this article, we will look at how to find and eliminate bottlenecks in PHP code using modern tools.



1. Main PHP profiling tools

There are several proven tools for profiling PHP. Let's look at the most popular ones:



1.1. Xdebug — the classic profiler

Xdebug is a PHP extension that provides powerful debugging and profiling capabilities. It generates trace files that can be analyzed in special utilities (e.g., KCacheGrind or Webgrind).

Installing Xdebug (Ubuntu/Debian):

sudo apt install php-xdebugsudo phpenmod xdebug

Configuration in php.ini:

xdebug.mode = profilexdebug.output_dir = /tmp/profilingxdebug.profiler_output_name = cachegrind.out.%t.%p

After this, each request will create a profile file in the /tmp/profiling folder. You can open it in KCacheGrind (Linux) or QCacheGrind (Windows/Mac).



1.2. Blackfire.io — cloud profiler

Blackfire is a commercial service from SensioLabs (the creators of Symfony). It offers a convenient web interface, CI/CD integration, and automatic optimization recommendations.

Installation via Composer:

composer require blackfire/php-sdk

Example of use in code:

<?php$probe = Blackfire::createProbe();// Your code$probe->close();?>

Blackfire shows not only execution time, but also the number of SQL queries, function calls, and memory consumption. It is ideal for large projects.



1.3. Built-in PHP functions

For quick diagnostics, you can use built-in functions:

  • microtime(true) — measure the execution time of a code section.
  • memory_get_usage() — current memory consumption.
  • memory_get_peak_usage() — peak memory consumption.

Example:

<?php$start = microtime(true);// Your code$time = microtime(true) - $start;echo "Executed in: " . round($time, 4) . " sec";?>


2. Typical bottlenecks in PHP code

After you have set up the profiler, it's time to understand what to look for. Here are the most common problems:



2.1. N+1 queries to the database

This is a classic: related data is selected in a loop with separate queries. Example of bad code:

<?php$users = $db->query("SELECT * FROM users");foreach ($users as $user) {    $posts = $db->query("SELECT * FROM posts WHERE user_id = {$user['id']}");    // Processing...}?>

Solution: one query with JOIN:

<?php$data = $db->query("SELECT u.*, p.* FROM users u LEFT JOIN posts p ON u.id = p.user_id");?>


2.2. Non-optimal loops

Nested loops with large arrays are a performance killer. Example:

<?phpforeach ($bigArray as $outer) {    foreach ($bigArray as $inner) {        // O(n²) complexity    }}?>

Use hash tables (associative arrays) to speed up lookups:

<?php$map = [];foreach ($bigArray as $item) {    $map[$item['id']] = $item;}?>


2.3. Excessive use of autoloading classes

Blogs

Book Recommendations