🇷🇺 Русский · 🇬🇧 English · 🇩🇪 Deutsch · 🇫🇷 Français · 🇪🇸 Español · 🇮🇹 Italiano
Stopwatch — stopwatch
Purpose
A monotonic hrtime() stopwatch: labelled laps, repeat-safe stop, elapsed(): TimeSpan, static measure(callable).
What for and how to use
Profiling code sections, measuring pipeline stages: $watch->lap('prepare'), Stopwatch::measure(fn () => heavy_work()).
Example
$watch = Stopwatch::start();
$watch->lap('preparation');
$watch->lap('processing');
$total = $watch->stop(); // float seconds
$span = $watch->elapsed(); // TimeSpan
$cost = Stopwatch::measure(fn () => $job->run());
Anti-patterns
Not for wall-clock time — the stopwatch deliberately knows no calendar.
Lifecycle
stateDiagram-v2
[*] --> Running: start()
Running --> Running: lap(label)
Running --> Stopped: stop()
Stopped --> Stopped: stop() (same result)
Stopped --> [*]: elapsed()/laps()
Feature comparison with competitors
| Aspect | 🏆 CloudCastle | symfony | carbon | lcobucci | beste | ergebnis |
|---|---|---|---|---|---|---|
Monotonic base (hrtime) | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ |
| Labelled laps | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ |
| One-line callable measurement | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ |
Result as TimeSpan | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ |
| Total | 🏆 4 | 0 | 0 | 0 | 0 | 0 |
🇷🇺 Русский · 🇬🇧 English · 🇩🇪 Deutsch · 🇫🇷 Français · 🇪🇸 Español · 🇮🇹 Italiano