PHPackages                             microtimer/microtimer - 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. [Debugging &amp; Profiling](/categories/debugging)
4. /
5. microtimer/microtimer

ActiveLibrary[Debugging &amp; Profiling](/categories/debugging)

microtimer/microtimer
=====================

The primary purpose of Microtimer is measuring time elapsed between two points (marks). On top of that you can pass extra data when marking a point in time, get a mark by key, or get all marks, or get a subset of marks. You can also measure a time between any arbitary mark or a timestamp.

1.0(8y ago)015MITPHPPHP &gt;=7.0

Since Jan 23Pushed 8y ago1 watchersCompare

[ Source](https://github.com/rinatkhaziev/microtimer)[ Packagist](https://packagist.org/packages/microtimer/microtimer)[ Docs](https://github.com/rinatkhaziev/microtimer)[ RSS](/packages/microtimer-microtimer/feed)WikiDiscussions master Synced 6d ago

READMEChangelogDependenciesVersions (2)Used By (0)

Microtimer
==========

[](#microtimer)

Dead simple PHP timing/profiling tool.

Description
-----------

[](#description)

Sometimes we need to measure performance impact of the code we run. There's nothing wrong with timing via `time()` or `microtime()` in your code, but you might need a little more. The primary purpose of Microtimer is measuring time elapsed between two points (marks). On top of that you can pass extra data when marking a point in time, get a mark by key, or get all marks, or get a subset of marks. You can also measure a time between any arbitary mark or a timestamp. You can run multiple timers on each request.

Why do I need this?
-------------------

[](#why-do-i-need-this)

There's [HRTime](http://php.net/manual/en/intro.hrtime.php), it provides high resolution (up to nanoseconds and ticks), it's more accurate because it uses low-level APIs. The problem is it's a PECL extension, and most likely you don't have it installed. There's also high chance that you won't be able to install it (shared hosting/managed hosting/IT department/you name it). With Microtimer you don't need to jump any hoops, just install via composer or copy the file in your project.

Usage
-----

[](#usage)

You can see a complete example further down.

### Create a new timer

[](#create-a-new-timer)

Create a new timer called 'Test Timer', set precision to 4. This will start a timer and add a first mark with key `start`.

```
$timer = Microtimer\Microtimer::create( 'Test Timer', 4 );
```

### Mark

[](#mark)

A mark is an associative array containing `timestamp`, `key`, and `data`.

`data` contains `since_start` and `since_last` (time elapsed since creation of the timer and time elapsed since the last mark), and any additional info you passed as the second argument.

```
array(3) {
  ["timestamp"]=>
  float(1516588452.1361)
  ["key"]=>
  string(6) "Mark 1"
  ["data"]=>
  array(3) {
    ["since_start"]=>
    float(0.0242)
    ["since_last"]=>
    float(0.0114)
    ["extra_information"]=>
    string(24)=>"extra_information"
  }
}
```

To make a new mark you can use either mark() method or calling the timer object as a function. The new mark is returned on success or an exception is thrown when timer has already been stopped.

```
// Shorthand
$timer('Mark Key', [ 'extra_information' => 'Anything you want to add', 'backtrace' => debug_backtrace() ]);
// Or
$mark = $timer->mark( 'Mark Key', [ 'extra_information' => 'Anything you want to add' ] );
// $logger->log( $mark );
```

### Stop the timer

[](#stop-the-timer)

Stops the timer, adds a mark with key `end`. No marks can be added after the timer has been stopped.

```
$timer->stop();
```

### Get a single mark

[](#get-a-single-mark)

```
var_dump( $timer->{'Mark 1'} )
```

### Get all or subset of marks based on their key

[](#get-all-or-subset-of-marks-based-on-their-key)

```
// All marks
$timer->marks();

// Search for a substring
// will match 'Mark 1', 'Mark 10', 'a Mark 1', etc
$timer->marks('Mark 1');
```

### Get time elapsed between two marks(or timestamps)

[](#get-time-elapsed-between-two-marksor-timestamps)

Arguments can be either a key, a timestamp, or a mark, order doesn't matter - Microtimer only returns absolute values. An exception will be thrown in the following cases:

- Not enough arguments
- A string is not valid/existing key
- Passed associative array doesn't have `timestamp` key

```
$timer->diff( $timer->{'Mark 1'}, $timer->{'Mark 7'} )
```

### Get time elapsed since the last mark

[](#get-time-elapsed-since-the-last-mark)

Accepts optional `$timestamp` argument, otherwise returns the difference between now and the last marker.

```
$timer->since_last();
```

### Complete example

[](#complete-example)

```
$timer = \Microtimer\Microtimer::create( 'Test Timer', 4 );

foreach( range( 0, 10 ) as $mark ) {
	usleep(15000);
	$timer( "Mark {$mark}" );
}

$since_last = $timer->since_last();

// Getting time elapsed between registered marks and/or arbitrary timestamps
var_dump( $timer->diff( $timer->{'Mark 1'}, $timer->{'Mark 7'} ) ); // -> float(0.1003)

var_dump( $timer->diff( microtime(true), $timer->{'Mark 7'} ) ); // -> float(0.0522)

var_dump( $timer->diff( 'Mark 1', $timer->{'Mark 10'} ) ); // -> float(0.152)
```

Installation
------------

[](#installation)

Install via composer

`# composer require microtimer\microtimer`

Alternatively, just copy `src/Microtimer.php` in your project and include it.

Requirements
------------

[](#requirements)

There's no other dependencies/requirements except PHP 7. If you still haven't upgraded, [you should definitely do so](http://php.net/supported-versions.php).

Notes
-----

[](#notes)

Microtimer uses *[strict types](http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration.strict)* so stuff like `$timer->mark( 0, '5' );` will throw a TypeError. For the exact method signatures please see the code.

Questions/comments/new features
-------------------------------

[](#questionscommentsnew-features)

Microtimer has absolute minimum of features, and it is intended to be kept that way. If you think that's something is missing, please create an issue first, and only submit a pull request after discussion (or you can fork it and do whatever you like).

Changelog
---------

[](#changelog)

- 1.0 - Initial Release

License
-------

[](#license)

MIT

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity58

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

Unknown

Total

1

Last Release

3037d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/3d2caf802fd178b2daff32c56b8ec72ffdf0f479894697e60e0511e143c98457?d=identicon)[rinatk](/maintainers/rinatk)

---

Top Contributors

[![rinatkhaziev](https://avatars.githubusercontent.com/u/459254?v=4)](https://github.com/rinatkhaziev "rinatkhaziev (2 commits)")

---

Tags

performanceprofilingtimertimestampstopwatchtiming

### Embed Badge

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

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

###  Alternatives

[composer/xdebug-handler

Restarts a process without Xdebug.

2.6k397.5M90](/packages/composer-xdebug-handler)[ayesh/php-timer

High-resolution and monotonic stop-watch for all your needs. Supports timer start, pause, resume, stop, read, and minimal conversion.

22226.4k11](/packages/ayesh-php-timer)[wikimedia/arc-lamp

Flame graphs and log processing for PHP stack traces.

434.4k](/packages/wikimedia-arc-lamp)[jsanc623/phpbenchtime

A lightweight benchmark timer and lap profiler for PHP.

2539.1k1](/packages/jsanc623-phpbenchtime)

PHPackages © 2026

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