Skip to content

Instantly share code, notes, and snippets.

@f2r
Created March 22, 2020 13:42
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 f2r/4f21279732cc1ba81dddc05ef042a1f5 to your computer and use it in GitHub Desktop.
Save f2r/4f21279732cc1ba81dddc05ef042a1f5 to your computer and use it in GitHub Desktop.
Benchmark UUID
<?php
require __DIR__.'/../vendor/autoload.php';
$uuid = '0d1913c6-5768-490c-a335-33d295237383';
$bytes = hex2bin(str_replace('-', '', $uuid));
final class Uuid {
/**
* @var string
*/
private $uuid;
public function __construct(string $uuid)
{
if (\uuid_is_valid($uuid) === false) {
throw new RuntimeException("Wrong UUID format");
}
$this->uuid = $uuid;
}
public function equals(Uuid $other): bool
{
return $this->uuid === $other->uuid;
}
public static function fromBytes(string $bytes): self
{
return new self(\uuid_unparse($bytes));
}
public function getBytes(): string
{
return \uuid_parse($this->uuid);
}
public function toString(): string
{
return $this->uuid;
}
}
final class Uuid2 {
/**
* @var string
*/
private $uuid;
public function __construct(string $uuid)
{
if (\Symfony\Polyfill\Uuid\Uuid::uuid_is_valid($uuid) === false) {
throw new RuntimeException("Wrong UUID format");
}
$this->uuid = $uuid;
}
public function equals(Uuid2 $other): bool
{
return $this->uuid === $other->uuid;
}
public static function fromBytes(string $bytes): self
{
return new self(\Symfony\Polyfill\Uuid\Uuid::uuid_unparse($bytes));
}
public function getBytes(): string
{
return \Symfony\Polyfill\Uuid\Uuid::uuid_parse($this->uuid);
}
public function toString(): string
{
return $this->uuid;
}
}
final class Uuid3 {
/**
* @var string
*/
private $uuid;
public function __construct(string $uuid)
{
if (\preg_match('`^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$`Di', $uuid) === 0) {
throw new RuntimeException("Wrong UUID format");
}
$this->uuid = $uuid;
}
public function equals(Uuid3 $other): bool
{
return $this->uuid === $other->uuid;
}
public static function fromBytes(string $bytes): self
{
if (\strlen($bytes) !== 16) {
throw new RuntimeException("Invalid binary UUID. Length is not 16 bytes");
}
$hex = \substr_replace(\bin2hex($bytes), '-', 8, 0);
$hex = \substr_replace($hex, '-', 13, 0);
$hex = \substr_replace($hex, '-', 18, 0);
return new self(\substr_replace($hex, '-', 23, 0));
}
public function getBytes(): string
{
return \hex2bin(\str_replace('-', '', $this->uuid));
}
public function toString(): string
{
return $this->uuid;
}
}
final class Uuid4 {
/**
* @var string
*/
private $uuid;
public function __construct(string $uuid, bool $check = true)
{
if ($check === true && \preg_match('`^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$`Di', $uuid) === 0) {
throw new RuntimeException("Wrong UUID format");
}
$this->uuid = $uuid;
}
public function equals(Uuid4 $other): bool
{
return $this->uuid === $other->uuid;
}
public static function fromBytes(string $bytes): self
{
if (\strlen($bytes) !== 16) {
throw new RuntimeException("Invalid binary UUID. Length is not 16 bytes");
}
$hex = \substr_replace(\bin2hex($bytes), '-', 8, 0);
$hex = \substr_replace($hex, '-', 13, 0);
$hex = \substr_replace($hex, '-', 18, 0);
return new self(\substr_replace($hex, '-', 23, 0), false);
}
public function getBytes(): string
{
return \hex2bin(\str_replace('-', '', $this->uuid));
}
public function toString(): string
{
return $this->uuid;
}
}
$count = $len = 10000000;
$start = microtime(true);
while ($count--) {
Ramsey\Uuid\Uuid::fromBytes($bytes)->getBytes();
}
echo "Ramsey => ", round((microtime(true)-$start)*1000), " ms\n";
$count = $len;
$start = microtime(true);
while ($count--) {
Uuid::fromBytes($bytes)->getBytes();
}
echo "ext-uuid => ", round((microtime(true)-$start)*1000), " ms\n";
$count = $len;
$start = microtime(true);
while ($count--) {
Uuid2::fromBytes($bytes)->getBytes();
}
echo "polyfill-symfony => ", round((microtime(true)-$start)*1000), " ms\n";
$count = $len;
$start = microtime(true);
while ($count--) {
Uuid3::fromBytes($bytes)->getBytes();
}
echo "custom => ", round((microtime(true)-$start)*1000), " ms\n";
$count = $len;
$start = microtime(true);
while ($count--) {
Uuid4::fromBytes($bytes)->getBytes();
}
echo "custom-without-check => ", round((microtime(true)-$start)*1000), " ms\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment