Skip to content

Instantly share code, notes, and snippets.

@edipretoro
Created January 25, 2016 12:37
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 edipretoro/1136df08c0a02debe990 to your computer and use it in GitHub Desktop.
Save edipretoro/1136df08c0a02debe990 to your computer and use it in GitHub Desktop.
Testing some ways to generate short GUID
#!/usr/bin/env perl
use strict;
use warnings;
use Data::GUID;
use Digest::CRC 'crc32';
use Data::Printer;
my %keys_hex;
my %keys_b64;
my $sample = shift(@ARGV) || 1_000_000;
for (my $cpt = 0 ; $cpt < $sample ; $cpt++) {
my $guid = Data::GUID->new();
my $crc_hex = crc32( $guid->as_hex() );
my $crc_b64 = crc32( $guid->as_base64() );
push @{$keys_hex{$crc_hex}}, $guid->as_string();
push @{$keys_b64{$crc_b64}}, $guid->as_string();
}
my $b64_collisions = 0;
map { $b64_collisions++ if scalar( @{$keys_b64{$_}} ) > 1 } keys %keys_b64;
my $hex_collisions = 0;
map { $hex_collisions++ if scalar( @{$keys_hex{$_}} ) > 1 } keys %keys_hex;
print "We have $b64_collisions collisions for the Base64 version.\n";
print "We have $hex_collisions collisions for the Hex version.\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment