in reply to RFC: Math::Base62
These are considerably more concise :
use constant BASE62 => join '', 'a'..'z', 0 .. 9, 'A'..'Z'; sub toBase62 { use integer; my $n = shift; my @out; push( @out, substr( BASE62, $n % 62, 1 ) ), $n /= 62 while $n; join '', @out; } sub fromBase62 { my $t = reverse shift; my $n = 0; $n *= 62, $n += index( BASE62, substr( $t, 0, 1, '') ) while lengt +h $t; return $n }
and quite a bit more efficient:
#! perl -slw use 5.010; use strict; use Benchmark qw[ cmpthese ]; use Math::Random::MT qw[ rand ]; ... our @data = map int( rand 2**32 ), 1 .. 1000; cmpthese -1, { yourn => q[ $_ == alphaID( alphaID( $_, 0 ), 1 ) or die "Yourn $_" for @data; ], mine => q[ $_ == fromBase62( toBase62( $_ ) ) or die "mine $_" for @data; ], }; __END__ C:\test>junk25 Rate yourn mine yourn 49.2/s -- -48% mine 93.9/s 91% --
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: RFC: Math::Base62
by isync (Hermit) on Oct 04, 2011 at 19:15 UTC |