PHPackages                             moznion/benchmarker - 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. moznion/benchmarker

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

moznion/benchmarker
===================

Benchmark running times of PHP code

v1.1.0(11y ago)0212MITPHPPHP &gt;=5.4.0

Since Sep 26Pushed 11y ago1 watchersCompare

[ Source](https://github.com/moznion/BenchMarker)[ Packagist](https://packagist.org/packages/moznion/benchmarker)[ Docs](https://github.com/moznion/BenchMarker)[ RSS](/packages/moznion-benchmarker/feed)WikiDiscussions master Synced 1mo ago

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

\#BenchMarker [![Build Status](https://camo.githubusercontent.com/fcd855a6a8b8f8b5985bd07d63d485993b7bf09a9900273b8e85ead8742f4d9b/68747470733a2f2f7472617669732d63692e6f72672f6d6f7a6e696f6e2f42656e63684d61726b65722e737667)](https://travis-ci.org/moznion/BenchMarker)

Benchmark running times of PHP code (inspired by [Benchmark.pm](http://perldoc.perl.org/Benchmark.html))

Synopsis
--------

[](#synopsis)

```
$benchmarker = new \Moznion\Benchmarker("all");

$code = function () use ($foo) {
    // do something
};

$benchmarker->timeThese(10000, [
    "code A" => function () use ($bar) {
        // do something
    },
    "code B" => $code, // it can also take variable of anonymous function
]);
// Sample of Output:
//    code A: 3.9326 wallclock secs (3sr + 0.2562ys = PU)
//    code B: 6.40877 wallclock secs (6sr + 0.81337ys = PU)

$benchmarker->cmpThese(10000, [
    "code A" => function () use ($bar) {
        // do something
    },
    "code B" => $code, // it can also take variable of anonymous function
]);
// Sample of Output:
//               Rate  code A  code B
//    code A  22571/s      --    -49%
//    code B  44656/s     98%      --
```

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

[](#description)

Library for benchmarking the running times of PHP code.

This library is inspired by [Benchmark of Perl](http://perldoc.perl.org/Benchmark.html)

Methods
-------

[](#methods)

### `new($style_name=null, $format=null, $enable_cache=null)`

[](#newstyle_namenull-formatnull-enable_cachenull)

Creates the instance of BenchMarker.

- `$style_name (String)`

Specify the name of style. Default value is `auto`.

Please see also [Style](https://github.com/moznion/BenchMarker#style).

- `$format (String)`

Specify the format to format the values of benchmark's result. It supports the format which is compatible with `sprintf()`'s one.

Default value is '5.2f'.

- `$enable_cache (Boolean)`

Specify cache the elapsed time of nop function (elapsed time of nop is used internal for accuracy). If you enable caching, processing time of benchmark will be faster.

Default value is 'false'.

### `timeIt($count, $code)`

[](#timeitcount-code)

Run a chunk of code and see how long it goes.

This method returns [instance of result-time](https://github.com/moznion/BenchMarker#result-time).

- `$count`

`$count` is the number of times to run the loop. This argument must be positive integer.

- `$code`

`$code` is the code to run. This argument must be callable variable.

### `countIt($time, $code)`

[](#countittime-code)

See how many times a chunk of code runs in a given time.

This method returns [instance of result-time](https://github.com/moznion/BenchMarker#result-time).

- `$time`

`$time` is the minimum length of time to run the loop.

- `$code`

`$code` is the code to run. This argument must be callable variable.

### `timeThis($count, $code, $title=null)`

[](#timethiscount-code-titlenull)

Run a chunk of code several times and print result of benchmark.

This method returns [instance of result-time](https://github.com/moznion/BenchMarker#result-time).

- `$count`

`$count` is the number of times to run the loop. This argument must be positive integer.

This argument can be zero or negative. This means the minimum number of CPU seconds to run. A zero signifies the default of 3 seconds.

- `$code`

`$code` is the code to run. This argument must be callable variable.

- `$title`

Title of result. `$title` defaults to "timethis $count".

### `timeThese($count, $codes)`

[](#timethesecount-codes)

Run several chunks of code several times and print result of benchmark.

This method returns [instance of result-time](https://github.com/moznion/BenchMarker#result-time) of array for each codes.

- `$count`

`$count` is the number of times to run the loop. This argument must be positive integer.

This argument can be zero or negative. This means the minimum number of CPU seconds to run. A zero signifies the default of 3 seconds.

- `$codes`

`$codes` is the array of codes to run.

### `cmpThese($count, $codes)`

[](#cmpthesecount-codes)

Print results of timeThese as a comparison chart.

- `$count`

`$count` is the number of times to run the loop. This argument must be positive integer.

This argument can be zero or negative. This means the minimum number of CPU seconds to run. A zero signifies the default of 3 seconds.

- `$codes`

`$codes` is the array of codes to run.

### `timeStr($time_result, $count=null)`

[](#timestrtime_result-countnull)

Print formatted time.

- `$time_result`

[instance of result-time](https://github.com/moznion/BenchMarker#result-time).

- `$count`

Number of loops. If this argument is not null then this method appends the rate.

### `timeDiff($new_time, $old_time)`

[](#timediffnew_time-old_time)

Calculates difference between `$new_time` and `$old_time`.

Arguments must be [instance of result-time](https://github.com/moznion/BenchMarker#result-time).

### `clearCache($count)`

[](#clearcachecount)

Clear the caching of nop function which is related $count.

### `clearAllCache()`

[](#clearallcache)

Clear all of the caching of nop function.

### `disableCache()`

[](#disablecache)

Disable to cache the caching of nop function.

### `enableCache()`

[](#enablecache)

Enable to cache the caching of nop function.

Result Time
-----------

[](#result-time)

Time class has 6 public members.

- `$real_time`

Real duration.

- `$sys_time`

Duration of system time.

- `$user_time`

Duration of user time.

- `$child_sys_time`

Duration of system time of child processes.

- `$child_user_time`

Duration of user time of child processes.

- `$count`

Times of loop to run.

Style
-----

[](#style)

This library supports 5 styles.

- all

Output all of the result of benchmarking.

- noc

Output the result of benchmarking without child processes result.

- nop

Output the result of benchmarking without parent processes result.

- none

Output nothing.

- auto

If child processes CPU time is bigger than 0 then this style is the same as "all". Elsewise, this style is the same as "noc".

Requires
--------

[](#requires)

PHP (version 5.4 or later)

See Also
--------

[](#see-also)

- [examples](https://github.com/moznion/BenchMarker/tree/master/eg)
- [Benchmark](http://perldoc.perl.org/Benchmark.html)

Notes
-----

[](#notes)

This library doesn't work on Microsoft Windows.

License
-------

[](#license)

MIT

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 91.4% 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 ~3 days

Total

2

Last Release

4240d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/a48e2ced7bca2b70e52623b16db094ebccda55894b8cc77baa49591eeccdd019?d=identicon)[moznion](/maintainers/moznion)

---

Top Contributors

[![moznion](https://avatars.githubusercontent.com/u/1422834?v=4)](https://github.com/moznion "moznion (32 commits)")[![uzulla](https://avatars.githubusercontent.com/u/870716?v=4)](https://github.com/uzulla "uzulla (2 commits)")[![sasezaki](https://avatars.githubusercontent.com/u/42755?v=4)](https://github.com/sasezaki "sasezaki (1 commits)")

---

Tags

benchmark

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[devster/ubench

Micro PHP library for benchmarking

5701.0M29](/packages/devster-ubench)[christophrumpel/artisan-benchmark

Benchmark Artisan Commands

17966.6k](/packages/christophrumpel-artisan-benchmark)[dragon-code/benchmark

Simple comparison of code execution speed between different options

11934.7k5](/packages/dragon-code-benchmark)[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)

PHPackages © 2026

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