Skip to content

Instantly share code, notes, and snippets.

@jaymecd
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaymecd/7dbac8908bacf54b6db6 to your computer and use it in GitHub Desktop.
Save jaymecd/7dbac8908bacf54b6db6 to your computer and use it in GitHub Desktop.
Benchmark UUID generation

Benchmark UUID generation

10'000 iterations with pecl-uuid: 1.0.3+, rhumsaa/uuid: 2.8 and ramsey/uuid: 3.0 implementations.

$ pecl install uuid
$ melody run uuid-benchmark.php

# no melody overhead for profiling
$ composer install && php uuid-benchmark.php

Results of benchmark:

    PECL | 0.0360 sec/10000 | 0.0000036 sec/one
 RHUMSAA | 0.0740 sec/10000 | 0.0000074 sec/one
  RAMSEY | 0.1840 sec/10000 | 0.0000184 sec/one
{
"name": "test/json",
"require": {
"ext-uuid": "~1.0",
"rhumsaa/uuid": "~2.8",
"ramsey/uuid": "~3.0@dev",
"symfony/stopwatch": "~2.2"
}
}
<?php
<<<CONFIG
packages:
- "rhumsaa/uuid: ~2.8"
- "ramsey/uuid: ~3.0@dev"
- "symfony/stopwatch: ~2.2"
CONFIG;
$selfName = basename(__FILE__);
if (basename($_SERVER['argv'][0]) === $selfName) {
if (!file_exists(__DIR__ . '/vendor/autoload.php')) {
echo 'Failure... Supported run methods:', PHP_EOL;
echo ' $ melody run ', $selfName, PHP_EOL;
echo ' $ composer install && php ', $selfName, PHP_EOL;
exit(1);
}
require_once __DIR__ . '/vendor/autoload.php';
}
$watch = new \Symfony\Component\Stopwatch\Stopwatch();
define('ITERATIONS', 10000);
$results = [];
$watch->start('pecl');
for ($i = 0; $i < ITERATIONS; ++$i) {
$x = uuid_create(UUID_TYPE_RANDOM);
}
$results['pecl'] = $watch->stop('pecl');
$watch->start('rhumsaa');
for ($i = 0; $i < ITERATIONS; ++$i) {
$x = (string) \Rhumsaa\Uuid\Uuid::uuid4();
}
$results['rhumsaa'] = $watch->stop('rhumsaa');
$watch->start('ramsey');
for ($i = 0; $i < ITERATIONS; ++$i) {
$x = (string) \Ramsey\Uuid\Uuid::uuid4();
}
$results['ramsey'] = $watch->stop('ramsey');
foreach ($results as $name => $result) {
printf('% 8s | %.04f sec/%d | %.07f sec/one' . PHP_EOL,
strtoupper($name),
$result->getDuration() / 1000,
ITERATIONS,
$result->getDuration() / 1000 / ITERATIONS);
}
@ramsey
Copy link

ramsey commented May 6, 2015

Thanks for putting this together.

This is interesting, since 3.0 should be using pecl/uuid, if it's present. Do you have any call grind data from your tests that we can use to identify the bottlenecks? If not, I'll try to replicate your tests this weekend and get that data for analysis.

@jaymecd
Copy link
Author

jaymecd commented May 7, 2015

@fabre-thibaud
Copy link

I'm actually suspecting the performance drop is due to the way the PeclUuidFactory is implemented: it checks on every call to uuidX() which method to use (pure PHP vs pecl) which probably creates a major overhead due to all the JMP's, though I may be wrong (I haven't checked the generated opcode's and cant tell much from the call graph).

I'll run some tests with an updated implementation that does not branch to see if that resolves the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment