PHP runtimeRuntime report7 repeated rounds

array_sum vs a PHP foreach accumulator

A controlled aggregation test over the same deterministic integer list.

Runtime investigation

Question and test setup

How does PHP’s built-in array_sum compare with an explicit foreach loop for summing a medium integer list?

Initial expectation

The built-in function may reduce interpreter-level loop overhead, while an explicit loop remains necessary when validation, filtering or overflow policy is part of the operation.

Built-ins can express intent clearly and execute optimized internal paths. Real aggregation code, however, often combines filtering, conversion and business rules.

Splitting one pass into several built-in calls can create extra arrays and erase an isolated advantage.

The measured gap

Lowest measured medianarray_sum
Median time9.47 µs
Median spread356.25%

What this run shows: array_sum recorded the lowest median for this numeric array. The absolute saving should be multiplied by real call frequency before changing readable code. The slowest-to-fastest median spread was 356.25% on the measured host.

Measured results. Lower median time is faster.
VariantMedianMiddle 50%Operations/sAdditional measure
array_sumFastest9.47 µs9.46 µs–9.90 µs105,580
foreach accumulator43.21 µs41.56 µs–53.40 µs23,141

What the numbers suggest

Use the built-in result as evidence for the exact operation it represents. Do not decompose a richer single-pass transformation into multiple arrays solely to call array_sum.

When the task is exactly summation, the built-in is both concise and easy to review. When the loop already performs necessary validation, one pass may be the better whole-system design.

When the difference is worth acting on

  • Use array_sum for a clean numeric list when summation is the complete operation.
  • Keep validation and filtering explicit at trust boundaries.
  • Measure allocation and total pipeline time when considering chained array functions.

Repetition and environment

  1. Both variants sum the same 5,000 deterministic integers.
  2. The explicit loop performs only accumulation so the semantic work matches array_sum.
  3. No filtering, type coercion policy or overflow handling is added.
180operations/sample
7measured rounds
2variants
Medianprimary statistic
Open measured environment
Measurement run
2026-07-22T15:23:56+00:00
PHP
8.4.23 (cli)
Operating system
Linux 4.18.0-553.85.1.el8_10.x86_64
Architecture
x86_64
Processor
AMD EPYC-Rome Processor
Reported host memory
15.02 GiB host total
CLI OPcache
0
PHP memory limit
1024M
Loaded study extensions
json, openssl, zlib, mbstring, pdo_sqlite
Timing clock
hrtime(true)
Measured rounds
7

Reasons the ranking may move

  • Only integers are tested.
  • Floating-point precision and mixed numeric strings are outside scope.

Primary documentation

These sources describe the PHP functions under test. The performance ranking comes only from this published fixture and environment.

Challenge the local result