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% --

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: RFC: Math::Base62
by isync (Hermit) on Oct 04, 2011 at 19:15 UTC
    Thanks for your effort and for sharing expert-quality code!

    (Anyone willing to package it for us for cpan, just append to this thread.)