PHPackages                             intellex/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. intellex/stopwatch

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

intellex/stopwatch
==================

v1.1.0(6y ago)11.8kMITPHPPHP &gt;=5.6

Since Mar 11Pushed 6y ago1 watchersCompare

[ Source](https://github.com/IntellexApps/stopwatch)[ Packagist](https://packagist.org/packages/intellex/stopwatch)[ RSS](/packages/intellex-stopwatch/feed)WikiDiscussions master Synced today

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

Lightweight PHP time measurement
================================

[](#lightweight-php-time-measurement)

Useful for debugging slow scripts and optimizing frameworks.

- Simple **start** / **stop** stopwatch
- Supports **nested** measurements
- **Aggregated** measurements for **repeating** functions
- Plain text **report**

Exception handling
------------------

[](#exception-handling)

The Stopwatch will throw an exception on any anomaly. However, by default these exceptions are silently ignored.

In order to set a custom handler use:

```
StopwatchException::setExceptionHandler(StopwatchExceptionHandling $handler);
```

For complete list of exceptions, see `/src/Exception` directory.

API
---

[](#api)

#### Simple usage

[](#simple-usage)

Use the pair of methods `Stopwatch::start(string $name)` and `Stopwatch::stop(string $name)` to measure elapsed time.
The `string $name` is used to name and identify the measurement and it must match across the two methods.

`Stopwatch::stop(string $name)` returns an instance `Measurement`, which holds the information about the measurement.

Example:

```
Stopwatch::start('Init');
// ...
$measurement = Stopwatch::stop('Init'); // Returns an instance of Measurement

// Available options
$measurement->getStart();    // The start time of the measurement, in milliseconds
$measurement->getEnd();      // The end time of the measurement in milliseconds
$measurement->getTime();     // The total time elapsed in milliseconds
```

#### Nested

[](#nested)

Nested measurements allow you to get more details where the time is spent, but also to easily know the total time spent. Level of nesting is not limited.

Example:

```
Stopwatch::start('Total');
    // ...
    Stopwatch::start('Init');
        // ...
    Stopwatch::stop('Init');

    Stopwatch::start('Database');
        Stopwatch::start('Connection');
            // ...
        Stopwatch::stop('Connection');

        Stopwatch::start('Read');
            // ...
            Stopwatch::start('Cache');
            	// ...
            Stopwatch::stop('Cache');
        Stopwatch::stop('Read');
    Stopwatch::stop('Database');
$measurement = Stopwatch::stop('Total');

// The total time elapsed in 'Total', in milliseconds
$measurement->getTime();

// The list of direct children measurements ('Database' and 'Rendering'), with same API as this measurement
$subMeasurements = $measurement->getChildren();
```

#### Aggregated

[](#aggregated)

If a certain function is called multiple times, measure the total time.

```
$loops = 10;
while ($loops--) {
	Stopwatch::startAggregate('loop');
		// ...
	Stopwatch::pauseAggregate('loop');
}

// Get the instance of ```
AggregateMeasurment
```
$aggregateMeasurement = Stopwatch::getAggregatedMeasurements()['loop'];

$aggregateMeasurement->getTotal()	// The total elapsed time, in milliseconds
$aggregateMeasurement->getCount()	// The number of loops
```

#### Print summary

[](#print-summary)

Print the summary of all measurements as plain text: `$measurement->report()`.

```

```

From the nested example above, will produce:

```
Total: 100
├───Init: 9
└───Database: 91
	├───Connection: 76
	└───Read: 15
		└───Cache: 3
```

TODO
----

[](#todo)

1. Tests.
2. Improve the print summary.

Licence
-------

[](#licence)

MIT License

Copyright (c) 2019 Intellex

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Credits
-------

[](#credits)

Script has been written by the [Intellex](https://intellex.rs/en) team.

###  Health Score

29

—

LowBetter than 57% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

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 ~91 days

Total

4

Last Release

2396d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/9035401?v=4)[Intellex](/maintainers/intellexapps)[@IntellexApps](https://github.com/IntellexApps)

### Embed Badge

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

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

###  Alternatives

[salmanzafar/laravel-geocode

A Laravel Library to find Lat and Long of a given Specific Address

154.4k](/packages/salmanzafar-laravel-geocode)

PHPackages © 2026

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