Whole-file reads vs 64 KiB streaming reads
A local filesystem comparison of file_get_contents and an explicit chunked fread loop on a 2 MiB fixture.
What the application is trying to do
For a modest local file, how do whole-file and chunked-read approaches compare on this host?
Choosing a file API is partly a resource-boundary decision. A fast whole-file read can still be the wrong design for unbounded uploads or multi-gigabyte exports.
Filesystem cache state can dominate small local tests, so multiple rounds and limitations are essential.
Observed host behavior
What this run shows: 64 KiB chunked fread completed this local read fixture sooner; peak memory, stream processing and file size determine whether whole-file loading is acceptable. The slowest-to-fastest median spread was 239.89% on the measured host.
| Variant | Median | Middle 50% | Operations/s | Additional measure |
|---|---|---|---|---|
| 64 KiB chunked freadFastest | 206.07 µs | 203.63 µs–209.56 µs | 4,853 | 2,097,152 input bytes |
| file_get_contents | 700.40 µs | 666.88 µs–733.86 µs | 1,428 | 2,097,152 input bytes |
What changes outside the fixture
Read speed for a cached 2 MiB file says little about safe memory use on a 2 GiB file. Use this result as a local baseline for modest artifacts only.
Chunking is often selected to cap memory or process data incrementally. Whole-file reads are convenient when an enforced size limit makes allocation safe.
Read path and controls
Whole-file reads may be efficient for modest files, while chunked reads provide bounded-memory behavior that matters more as files grow.
- Both variants read the same 2 MiB local fixture and count all returned bytes.
- The streaming path uses 64 KiB chunks and closes the handle each operation.
- The test does not flush the operating-system page cache between samples.
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
Before changing a production path
- Enforce a maximum file size before using whole-file reads on external input.
- Use streaming when files are large, unbounded or can be processed incrementally.
- Benchmark storage-backed workloads with representative cache state and concurrency.
Storage variables not simulated here
- Operating-system page cache is not cleared.
- Remote storage, network filesystems and concurrent readers are not represented.
Primary documentation
These references document the PHP filesystem and hashing operations. Local storage behavior remains specific to the measured host.