Content · Web Development · PHP

PHP Performance Basics: Finding Slow Code

Measure request duration and memory use around actual work, distinguish CPU from network latency, and profile representative workloads before optimizing.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

Measure request duration and memory use around actual work, distinguish CPU from network latency, and profile representative workloads before optimizing.

Before using it, identify which data, connection or runtime state the operation reads or changes. This avoids examples that appear to work but apply to a different context.

How Does This Work?


Measure request duration and memory use around actual work, distinguish CPU from network latency, and profile representative workloads before optimizing.

Syntax:

See the complete steps in the practical examples below.

Parameters and Return Value


This is an application workflow rather than one built-in function. Its inputs are request fields, configuration and application state described in each example.

The operation produces an HTTP response or updates application state as shown in the examples.

Example 1: Basic Usage


<?php

$startedAt = hrtime(true);
$sampleValues = range(1, 10000);
echo (hrtime(true) - $startedAt) / 1e6 . " milliseconds";

Measure request duration and memory use around actual work, distinguish CPU from network latency, and profile representative workloads before optimizing.

Example 2: Another Case


<?php

$startingMemory = memory_get_usage();
$sampleValues = array_fill(0, 1000, "report");
echo memory_get_usage() - $startingMemory;

The second example changes the input or surrounding conditions, which matters when choosing this API for real application code.

Practical Example


<?php

$measurements = [];
foreach (range(1, 3) as $runNumber) { $started = hrtime(true); array_sum(range(1, 1000)); $measurements[] = (hrtime(true) - $started)/1e6; }
print_r($measurements);

The practical example is a teaching scenario. Change sample configuration and credentials before using a similar design in an application.

Important Behavior and Edge Cases


  • Apply authentication, authorization, input limits and failure handling where the workflow crosses trust boundaries.
  • Relevant PHP environment: PHP core. Ensure the required extension and operating-system capabilities are present.
  • Check results and release resources where necessary; successful syntax alone does not guarantee a successful operation.

Comparison With Related Functions


OptionWhen to Use It
PHP Performance Basics: Finding Slow CodeMeasure request duration and memory use around actual work, distinguish CPU from network latency, and profile representative workloads before optimizing.
PHP web application designUse this related operation for its different responsibilities; do not assume identical inputs or return values.

Common Beginner Mistakes


MistakeCorrect Approach
Assuming a successful return valueInspect the returned value and handle failures according to this operation’s contract.
Using the API without its required environmentSupply the required resources, runtime configuration and permissions before calling it.
Copying demonstration values into productionUse application-specific validated values, secure configuration and safe error handling.

Key Takeaways


  • Measure request duration and memory use around actual work, distinguish CPU from network latency, and profile representative workloads before optimizing.
  • Apply authentication, authorization, input limits and failure handling where the workflow crosses trust boundaries.
  • Validate inputs and handle failure conditions before integrating this operation into a production workflow.

Measure request duration and memory use around actual work, distinguish CPU from network latency, and profile representative workloads before optimizing. Use the appropriate runtime and treat any environment-dependent output as an example, not a guaranteed result.

Share this resourceSend this page to someone who may find it useful.