UUID generation in PHP
We have been using UUID for years and different ways to generate UUID exist.
I prefer the PECL extension over the ramsey/uuid because it’s simpler and more straightforward. Few weeks ago, with Nicolas Grekas we had the idea to port the PECL extension to plain PHP as a Symfony Polyfill. Then, we wanted to blog about FFI, a new PHP 7.4 feature, so with Baptiste Leduc we bound the libuuid
to PHP.
So now, there are many ways to generate UUID in PHP, at least 4. Let’s compare them:
- The PECL extension;
- A polyfill for the PECL extension;
- An abstraction layer on top of many generator and an UUID component: ramsey/uuid;
- A FFI binding for PHP 7.4+.
I created a benchmark to see how theses implementations compare:
Section intitulée uuid-v1-time-basedUUID V1 (time based)
+----------------------+---------+-------+
| subject | mean | diff |
+----------------------+---------+-------+
| benchPecl | 0.637μs | 1.00x |
| benchSymfonyPolyfill | 1.736μs | 2.72x |
| benchJoliCodeFFI | 1.349μs | 2.12x |
| benchRamsey | 4.295μs | 6.74x |
+----------------------+---------+-------+
Section intitulée uuid-v4-random-valuesUUID V4 (random values)
+----------------------+---------+-------+
| subject | mean | diff |
+----------------------+---------+-------+
| benchPecl | 4.546μs | 2.87x |
| benchSymfonyPolyfill | 1.583μs | 1.00x |
| benchJoliCodeFFI | 5.416μs | 3.42x |
| benchRamsey | 2.103μs | 1.33x |
+----------------------+---------+-------+
Section intitulée how-to-explain-these-numbersHow to explain these numbers?
Section intitulée for-the-v1For the V1
V1 is time based. It means there are not so much random data. But each UUID must be unique, so an internal counter is required.
- The PECL is the fastest here because it uses some features of the kernel to get incremental data,
- The FFI binding is slower because there is a significant overhead with FFI for things that are really fast in the underlying library. This overhead comes from the conversion type between PHP and the underlying library,
- The Polyfill is slower because it must deal with the counter manually,
- The Rasmey implementation is the slowest because the way it deals with counter could be improved.
Section intitulée for-the-v4For the V4
V4 is full of random data. It means it is really expensive for the CPU to generate one compared to a V1.
- The Polyfill is the fastest because of PHP. PHP uses
sys_getrandom()
a faster way to get random data. - The Rasmey implementation is also very fast for the same reason,
- The PECL is much slower due to the underlying library (
libuuid
) opening a new file descriptor on/dev/urandom
each time we calluuid_create()
from PHP Code, - The FFI binding is the slowest since it relies on the same
libuuid
library, and it adds the FFI overhead.
Section intitulée conclusionConclusion
Now you now know everything about uuid, so… just make the right choice. ;)
Keep in mind that the FFI binding works only with PHP 7.4+, so it’s likely unusable for many projects (for now).
ramsey/uuid is an UUID component that leverage many uuid generator (it can use the PECL internally). It could be interesting to use the polyfill in the ramsey/uuid implementation.
And since the polyfill is the same thing than the PECL extension, the final choice is about the API:
- something simple: the PECL (or the polyfill if you can’t install an extension),
- something more feature-rich: the ramsey/uuid component,
- something hype: the FFI binding.
Now you know everything about the ways to generate UUID in PHP, just use the right one for you.
Commentaires et discussions
PHP 7.4 FFI: What you need to know
(🇫🇷 Lire la version en Français ici) PHP Foreign Function Interface, or FFI for fans, is a PHP extension that allows you to include with ease some externals libraries into your PHP code. That means it’s possible to use C, Go, Rust, etc. shared library directly in PHP without writing…
Lire la suite de l’article PHP 7.4 FFI: What you need to know
PHP 7.4 et FFI, ce qu’il faut retenir
(🇬🇧 Read the english version here) PHP Foreign Function Interface, ou PHP FFI pour les intimes, ou FFI pour les fans, est une extension PHP qui permet d’inclure facilement des bibliothèques externes au sein de PHP. Autrement dit, il est possible d’utiliser directement des librairies…
Lire la suite de l’article PHP 7.4 et FFI, ce qu’il faut retenir
Nos articles sur le même sujet
Nos formations sur ce sujet
Notre expertise est aussi disponible sous forme de formations professionnelles !

Symfony avancée
Découvrez les fonctionnalités et concepts avancés de Symfony
Ces clients ont profité de notre expertise

Canal+ a sollicité l’expertise technique de JoliCode dans le cadre d’un audit technique du framework PHP employé par l’entreprise pour ses développements. À l’aide de notre outillage projet, nous avons évalué la qualité du framework et son adéquation avec l’écosystème PHP actuel, et émis une série de recommandations en vue de la modernisation du socle…

JoliCode a formé l’équipe de développement d’Evaneos aux bonnes pratiques pour l’écriture de tests unitaires efficaces et utiles. Nous en avons également profité pour mettre en place une plateforme d’intégration continue pour accompagner l’évolution de la plateforme.

Dans le cadre du renouveau de sa stratégie digitale, Orpi France a fait appel à JoliCode afin de diriger la refonte du site Web orpi.com et l’intégration de nombreux nouveaux services. Pour effectuer cette migration, nous nous sommes appuyés sur une architecture de type “micro-services” à l’aide de PHP, Symfony, RabbitMQ, Elasticsearch et…