PHPackages                             sbwerewolf/stopwatch - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. sbwerewolf/stopwatch

ActiveLibary[Utility &amp; Helpers](/categories/utility)

sbwerewolf/stopwatch
====================

Primitive stopwatch with simple start and stop for measurement of every single time periods and whole time

v1.2.3(3y ago)014MITPHP

Since Feb 7Pushed 3y ago1 watchersCompare

[ Source](https://github.com/SbWereWolf/stopwatch)[ Packagist](https://packagist.org/packages/sbwerewolf/stopwatch)[ RSS](/packages/sbwerewolf-stopwatch/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (1)Versions (7)Used By (0)

Simple stopwatch
================

[](#simple-stopwatch)

Primitive stopwatch, with simple start and stop methods, for measurement of every single time periods and all time whole.

How to install
--------------

[](#how-to-install)

`composer require sbwerewolf/stopwatch`

How to use
----------

[](#how-to-use)

```
echo 'Duration is ' .
    (new \SbWereWolf\Stopwatch\HRTimeStopwatch())
    ->start()->stop()->getLastTime()->asNanoSeconds() .
    ' ns' .
    PHP_EOL;
```

```
Duration is 300 ns
```

Stopwatch available with different engines:

- `hrtime()`
- `microtime()`
- `DateTimeImmutable`

`\SbWereWolf\Stopwatch\HRTimeStopwatch` implements `hrtime()` engine.

`\SbWereWolf\Stopwatch\MicroTimeStopwatch` implements `microtime()` engine.

`\SbWereWolf\Stopwatch\DateTimeStopwatch` implements `DateTimeImmutable` engine.

Advanced usage
--------------

[](#advanced-usage)

```
$stopwatch = new SbWereWolf\Stopwatch\HRTimeStopwatch();
echo (new DateTimeImmutable())->format('s.u') . PHP_EOL;

$stopwatch->start();
time_nanosleep(0, 100);
$stopwatch->stop();

echo (new DateTimeImmutable())->format('s.u') .
    ' Period 1 duration: ' .
    $stopwatch->getLastTime()->asNanoSeconds() .
    ' ns' .
    PHP_EOL;

time_nanosleep(0, 100);

$stopwatch->start();
time_nanosleep(0, 100);
$stopwatch->stop();

echo (new DateTimeImmutable())->format('s.u') .
    ' Period 2 duration: ' .
    $stopwatch->getLastTime()->asNanoSeconds() .
    ' ns' .
    PHP_EOL;

/* Summa of all time periods */
echo (new DateTimeImmutable())->format('s.u') .
    ' Periods 1 + 2 summary duration: ' .
    $stopwatch->getSummaryTime()->asNanoSeconds() .
    ' ns' .
    PHP_EOL;

/* Overall time */
echo (new DateTimeImmutable())->format('s.u') .
    ' Whole process duration: ' .
    $stopwatch->getWholeTime()->asNanoSeconds() .
    ' ns' .
    PHP_EOL;
```

```
02.380004
02.380020 Period 1 duration: 12400 ns
02.380032 Period 2 duration: 3600 ns
02.380035 Periods 1 + 2 summary duration: 16000 ns
02.380044 Whole process duration: 24000 ns
```

Using a stopwatch to benchmark any process
------------------------------------------

[](#using-a-stopwatch-to-benchmark-any-process)

```
$stopwatch = new SbWereWolf\Stopwatch\HRTimeStopwatch();
$benchmark = new SbWereWolf\Stopwatch\Benchmark($stopwatch);

$delay = 100;
echo "Variable value before step z callback `$delay`" . PHP_EOL;
/* Variable value before step z callback `100` */
$benchmark->step('z', function () use ($delay) {
    time_nanosleep(0, $delay);
    $delay++;
});
echo "after step z callback `$delay`" . PHP_EOL;
/* after step z callback `100` */
/* Variable does not change its value */

$benchmark->step('x', function () use (&$delay) {
    time_nanosleep(0, $delay);
    $delay += 999;
});
echo "after step x callback `$delay`" . PHP_EOL;
/* after step x callback `1099` */
/* Variable does change its value */

$benchmark->step('c', function () use (&$delay) {
    $delay -= 999;
    time_nanosleep(0, $delay);
});
echo "after step c callback `$delay`" . PHP_EOL;
/* after step c callback `100` */
/* Variable does change its value */

echo "Benchmark steps measurement is:" . PHP_EOL;
$i=0;
foreach ($benchmark->report() as $desc => $val) {
    /** @var SbWereWolf\Stopwatch\ITimerReadings $val */
    echo "$desc => {$val->asNanoSeconds()} ns" . PHP_EOL;
    $i++;
}

$totalNanoseconds = $benchmark->total()->asNanoSeconds();
echo "Total is $totalNanoseconds ns";
```

```
variable value before step z `100`
after step z `100`
after step x `1099`
after step c `100`
Benchmark steps measurement is:
z => 15800 ns
x => 7600 ns
c => 4100 ns
Total is 27500 ns
```

Contacts
--------

[](#contacts)

```
Volkhin Nikolay
e-mail ulfnew@gmail.com
phone +7-902-272-65-35
Telegram @sbwerewolf

```

Chat with me via messenger

- [Telegram chat with me](https://t.me/SbWereWolf)
- [WhatsApp chat with me](https://wa.me/79022726535)

###  Health Score

23

—

LowBetter than 26% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

6

Last Release

1239d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/56415ea6aeceece7248ce372ec911905cab2cd1313ed2bb4ec8cfacdbaa7d08f?d=identicon)[SbWereWolf](/maintainers/SbWereWolf)

---

Top Contributors

[![SbWereWolf](https://avatars.githubusercontent.com/u/16444541?v=4)](https://github.com/SbWereWolf "SbWereWolf (6 commits)")

---

Tags

durationhrtimemicrotimephp-librarystopwatch

### Embed Badge

![Health badge](/badges/sbwerewolf-stopwatch/health.svg)

```
[![Health](https://phpackages.com/badges/sbwerewolf-stopwatch/health.svg)](https://phpackages.com/packages/sbwerewolf-stopwatch)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
