Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ static: vendor
vendor/bin/phpstan analyse src --level max

bench: vendor
vendor/bin/phpbench run benchmarks/ --report=default
vendor/bin/phpbench run benchmarks/ --report=short --group=encoder
vendor/bin/phpbench run benchmarks/ --report=short --group=decoder

watch: vendor
find . -name "*.php" -not -path "./vendor/*" -o -name "*.json" -not -path "./vendor/*" | entr -c make test
Expand Down
40 changes: 22 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,27 +70,31 @@ Install GMP if you can. It is much faster pure PHP encoder. Below benchmarks are

```
$ php --version
PHP 8.0.7 (cli) (built: Jun 4 2021 03:50:01) ( NTS )
PHP 8.3.20 (cli) (built: Apr 8 2025 20:21:18) (NTS gcc x86_64)

$ make bench

+-----------------------+------------------+-----------+
| subject | mean | diff |
+-----------------------+------------------+-----------+
| benchGmpDecoder | 140,409.997ops/s | 1.10x |
| benchGmpDecoderCustom | 154,607.297ops/s | 1.00x |
| benchPhpDecoder | 721.147ops/s | 214.39x |
| benchBcmathDecoder | 72.191ops/s | 2,141.64x |
+-----------------------+------------------+-----------+

+-----------------------+------------------+-----------+
| subject | mean | diff |
+-----------------------+------------------+-----------+
| benchGmpEncoder | 352,609.309ops/s | 1.00x |
| benchGmpEncoderCustom | 350,140.056ops/s | 1.01x |
| benchPhpEncoder | 669.959ops/s | 526.31x |
| benchBcmathEncoder | 72.956ops/s | 4,833.21x |
+-----------------------+------------------+-----------+
+--------------------------+------------------+
| subject | mode |
+--------------------------+------------------+
| benchGmpEncoder | 974,372.664ops/s |
| benchGmpEncoderCustom | 887,630.496ops/s |
| benchPhpEncoder | 1,993.159ops/s |
| benchPhpEncoderCustom | 1,978.154ops/s |
| benchBcmathEncoder | 155.390ops/s |
| benchBcmathEncoderCustom | 155.303ops/s |
+--------------------------+------------------+

+--------------------------+------------------+
| subject | mode |
+--------------------------+------------------+
| benchGmpDecoder | 289,200.539ops/s |
| benchGmpDecoderCustom | 278,208.793ops/s |
| benchPhpDecoder | 2,030.965ops/s |
| benchPhpDecoderCustom | 2,070.158ops/s |
| benchBcmathDecoder | 148.407ops/s |
| benchBcmathDecoderCustom | 148.725ops/s |
+--------------------------+------------------+
```

## Static Proxy
Expand Down
45 changes: 43 additions & 2 deletions benchmarks/Base62Bench.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/*
* This file is part of the Base62 package
*
* Copyright (c) 2016-2021 Mika Tuupola
* Copyright (c) 2016-2026 Mika Tuupola
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
Expand All @@ -30,17 +30,22 @@ class Base62Bench
{
private $data;
private $gmp;
private $gmp2;
private $php;
private $php2;
private $bcmath;
private $bcmath2;
private $encoded;

public function init()
{
$this->data = random_bytes(128);
$this->gmp = new GmpEncoder;
$this->gmp2 = new GmpEncoder(["characters" => Base62::INVERTED]);
$this->gmp2 = new GmpEncoder(Base62::INVERTED);
$this->php = new PhpEncoder;
$this->php2 = new PhpEncoder(Base62::INVERTED);
$this->bcmath = new BcmathEncoder;
$this->bcmath2 = new BcmathEncoder(Base62::INVERTED);
$this->encoded = $this->php->encode($this->data);
}

Expand Down Expand Up @@ -71,6 +76,15 @@ public function benchPhpEncoder()
$encoded = $this->php->encode($this->data);
}

/**
* @Revs(100)
* @Groups({"encoder"})
*/
public function benchPhpEncoderCustom()
{
$encoded = $this->php2->encode($this->data);
}

/**
* @Revs(100)
* @Groups({"encoder"})
Expand All @@ -80,6 +94,15 @@ public function benchBcmathEncoder()
$encoded = $this->bcmath->encode($this->data);
}

/**
* @Revs(100)
* @Groups({"encoder"})
*/
public function benchBcmathEncoderCustom()
{
$encoded = $this->bcmath2->encode($this->data);
}

/**
* @Revs(100)
* @Groups({"decoder"})
Expand Down Expand Up @@ -107,6 +130,15 @@ public function benchPhpDecoder()
$encoded = $this->php->decode($this->encoded);
}

/**
* @Revs(100)
* @Groups({"decoder"})
*/
public function benchPhpDecoderCustom()
{
$encoded = $this->php2->decode($this->encoded);
}

/**
* @Revs(100)
* @Groups({"decoder"})
Expand All @@ -115,4 +147,13 @@ public function benchBcmathDecoder()
{
$encoded = $this->bcmath->decode($this->encoded);
}

/**
* @Revs(100)
* @Groups({"decoder"})
*/
public function benchBcmathDecoderCustom()
{
$encoded = $this->bcmath2->decode($this->encoded);
}
}
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
"require-dev": {
"squizlabs/php_codesniffer": "^3.0",
"phpunit/phpunit": "^7.0|^8.0|^9.0",
"phpbench/phpbench": "^0.15.0|1.0.0-alpha3",
"overtrue/phplint": "^2.3",
"phpbench/phpbench": "^1.4",
"overtrue/phplint": "^9.1",
"phpstan/phpstan": "^1.12"
},
"suggest": {
Expand Down
13 changes: 7 additions & 6 deletions phpbench.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"bootstrap": "vendor/autoload.php",
"reports": {
"default": {
"generator": "table",
"cols": [ "subject", "mean", "diff" ],
"break": ["groups"]
"$schema": "./vendor/phpbench/phpbench/phpbench.schema.json",
"runner.bootstrap": "vendor/autoload.php",
"runner.path": "benchmarks",
"report.generators": {
"short": {
"extends": "aggregate",
"cols": ["subject", "mode"]
}
}
}