Sunday, November 28, 2010

Advent, Advent, a candle burns - Perl Advent Calendars

It is this time of the year again...

A few days ago I had a crazy idea: Why not publish a Perl Advent calendar? Every day I will write a short article about a CPAN module. I will feature some very small entries and some bigger ones. So, stay tuned for December 1st ...

Here is a list of other Perl Advent calendars:
 I wish you a happy first Advent.

Friday, November 26, 2010

Benchmark of UUID generation with Perl

At work the topic of UUID generation came up and our current solution with Data::UUID was too slow.

My initial thoughts were "there must exist a XS module for that". A colleague suggested OSSP uuid. On CPAN I found some more.

#!perl

use strict;
use warnings;

use Benchmark qw/cmpthese/;
use Data::UUID ();
use Data::UUID::LibUUID ();
use OSSP::uuid qw/:all/;
use UUID ();

my %bench = (
    data_uuid => \&data_uuid,
    libuuid   => \&libuuid,
    ossp      => \&ossp,
    uuid      => \&uuid,
);

foreach my $name (sort keys %bench) {
    printf("%10s: %s\n", $name, $bench{$name}->());
}
print "\n";

cmpthese($ARGV[0] || 100_000, \%bench);

sub data_uuid {
    return Data::UUID->new->create_str;
}

sub libuuid {
    return Data::UUID::LibUUID::new_uuid_string;
}

sub ossp {
    my ($uuid, $string);
    uuid_create($uuid);
    uuid_make($uuid, UUID_MAKE_V1);
    uuid_export($uuid, UUID_FMT_STR, $string, undef);
    uuid_destroy($uuid);
    return $string;
}

sub uuid {
    my ($uuid, $string);
    UUID::generate($uuid);
    UUID::unparse($uuid, $string);
    return $string;
}

The results are overwhelming and show a clear winner (and loser):

> perl uuid.pl
 data_uuid: 69F84998-F99F-11DF-B54E-5B24670C5CFA
   libuuid: 69f8a03c-f99f-11df-b8e9-0019db688fc2
      ossp: 69f8a672-f99f-11df-84f9-0019db688fc2
      uuid: 21795165-7d69-42fd-88f5-6f03c27bb595

              Rate data_uuid      ossp   libuuid      uuid
data_uuid   3189/s        --      -93%      -96%      -98%
ossp       46729/s     1365%        --      -40%      -71%
libuuid    78125/s     2350%       67%        --      -52%
uuid      161290/s     4958%      245%      106%        --

So, expect some changes next week... :)