Created
December 3, 2021 10:10
-
-
Save sergix44/e05be1a047c5d7c92c18dbd73cb30386 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| use JsonMapper\Cache\ArrayCache; | |
| use JsonMapper\Handler\PropertyMapper; | |
| require 'vendor/autoload.php'; | |
| function bench($what = 'Bench', $echo = true) | |
| { | |
| global $DEBUG_TIME; | |
| if ($DEBUG_TIME === null) { | |
| $DEBUG_TIME = hrtime(true); | |
| } else { | |
| $t = (hrtime(true) - $DEBUG_TIME) / 1e+9; | |
| $DEBUG_TIME = null; | |
| if ($echo) { | |
| echo "{$what} took {$t} s\n"; | |
| } else { | |
| return $t; | |
| } | |
| } | |
| return null; | |
| } | |
| class Address | |
| { | |
| public ?string $street; | |
| public ?string $city; | |
| } | |
| class Contact | |
| { | |
| /** | |
| * Full name | |
| * @var string|null | |
| */ | |
| public ?string $name; | |
| /** | |
| * @var Address|null | |
| */ | |
| public ?Address $address; | |
| } | |
| $json = json_decode('{"name":"Mario Rossi","address":{"street":"Via XX XX 1","city":"Milano"}}', false, 512, JSON_THROW_ON_ERROR); | |
| $mapper1 = new JsonMapper(); | |
| $mapper2 = (new \JsonMapper\JsonMapperFactory())->bestFit(); | |
| bench('cweiske/jsonmapper json mapper'); | |
| for ($i = 0; $i < 100000; $i++) { | |
| $o = new Contact(); | |
| $mapper1->map($json, $o); | |
| } | |
| bench('cweiske/jsonmapper json mapper'); | |
| bench('new json mapper "best fit"'); | |
| for ($i = 0; $i < 100000; $i++) { | |
| $o = new Contact(); | |
| $mapper2->mapObject($json, $o); | |
| } | |
| bench('jsonmapper/jsonmapper json mapper "best fit"'); | |
| $mapper2 = (new \JsonMapper\JsonMapperFactory())->default(); | |
| bench('new json mapper "default"'); | |
| for ($i = 0; $i < 100000; $i++) { | |
| $o = new Contact(); | |
| $mapper2->mapObject($json, $o); | |
| } | |
| bench('jsonmapper/jsonmapper json mapper "default"'); | |
| $c=new ArrayCache(); | |
| $mapper2 = (new \JsonMapper\JsonMapperFactory())->create( | |
| new PropertyMapper, | |
| new \JsonMapper\Middleware\NamespaceResolver($c), | |
| new \JsonMapper\Middleware\TypedProperties($c), | |
| ); | |
| bench('jsonmapper/jsonmapper json mapper "custom"'); | |
| for ($i = 0; $i < 100000; $i++) { | |
| $o = new Contact(); | |
| $mapper2->mapObject($json, $o); | |
| } | |
| bench('jsonmapper/jsonmapper json mapper "custom"'); | |
| /* | |
| composer.json | |
| { | |
| "require": { | |
| "json-mapper/json-mapper": "^2.9", | |
| "netresearch/jsonmapper": "^4.0" | |
| } | |
| } | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment