Created
January 25, 2016 12:37
-
-
Save edipretoro/1136df08c0a02debe990 to your computer and use it in GitHub Desktop.
Testing some ways to generate short GUID
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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