PHP Performance: How to Profile and Speed Up Code 10x

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

Your PHP site is slow? Time to look under the hood

\n\n

Have you ever waited for a page to load longer than it takes to drink your morning coffee? A familiar pain. PHP is a powerful language, but without proper configuration, it can turn into a turtle. In this article, we won't give boring lectures. We'll take real code, profile it, find bottlenecks, and speed it up so much that your server will thank you.

\n\n

We'll talk about PHP performance — from simple measurements to deep profiling. You'll learn how to find problem areas using Xdebug and Blackfire, and which optimization methods actually work. Let's go!

\n\n

Step 1. Measure everything: from microseconds to megabytes

\n\n

Before optimizing anything, you need to understand where the slowdown is. Intuition often fails. Let's use tools.

\n\n

The simplest way: microtime(true)

\n\n

For a quick check of a code snippet, good old microtime() will do. Not elegant, but it works.

\n\n
<?php\n$start = microtime(true);\n\n// Your slow code\n$data = [];\nfor ($i = 0; $i < 100000; $i++) {\n    $data[] = md5($i . 'salt');\n}\n\n$end = microtime(true);\necho 'Execution time: ' . round(($end - $start) * 1000, 2) . ' ms';\n?>
\n\n

This method is good for a rough estimate. But when a project grows, you need serious tools.

\n\n

Xdebug — your personal detective

\n\n

Xdebug is the de facto standard for PHP profiling. It generates cachegrind files that can be opened in KCachegrind (Linux) or QCachegrind (Mac/Windows).

\n\n

Installation via pecl:

\n
pecl install xdebug
\n\n

Enable profiling in php.ini:

\n
xdebug.mode = profile\nxdebug.output_dir = /tmp/profiling
\n\n

After running the script, a file will appear in the folder. Open it in QCachegrind — you'll see which function consumed the most time. Often a surprise: not the database, but simple string processing in a loop.

\n\n

Blackfire — for those who love beautiful dashboards

\n\n

Blackfire from SensioLabs (creators of Symfony) is a cloud profiler. It shows not only time but also the number of calls, memory, I/O. It integrates with PHPUnit, Symfony, Laravel.

\n\n

Example usage from the command line:

\n
blackfire run php myscript.php
\n\n

You'll get a link to a web report with visualization. Convenient for finding PHP performance bottlenecks.

\n\n

Step 2. Common problems and their solutions

\n\n

Now that we know how to profile, let's look at common pitfalls.

\n\n

Problem 1: N+1 queries in a loop

\n\n

A classic. You get a list of users, and then in a loop, you make a database query for each one.

\n\n

Slow code:

\n
<?php\n$users = $db->query('SELECT id FROM users LIMIT 100');\nforeach ($users as $user) {\n    $orders = $db->query("SELECT * FROM orders WHERE user_id = {$user['id']}");\n    // ...\n}\n?>
\n\n

Optimization:

\n
<?php\n$users = $db->query('SELECT id FROM users LIMIT 100');\n$ids = array_column($users, 'id');\n$orders = $db->query('SELECT * FROM orders WHERE user_id IN (' . implode(',', $ids) . ')');\n// Group by user_id\n$grouped = [];\nforeach ($orders as $order) {\n    $grouped[$order['user_id']][] = $order;\n}\n?>
\n\n

Result: 100 queries turned into 2. PHP application performance increases many times over.

\n\n

Problem 2: Inefficient loops and string operations

\n\n

String concatenation in a loop is evil. Use arrays and implode().

\n\n
<?php\n// Bad\n$result = '';\nfor ($i = 0; $i < 10000; $i++) {\n    $result .= 'item' . $i . ',';\n}\n\n// Good\n$parts = [];\nfor ($i =                

Blogs

Book Recommendations