Skip to content

Instantly share code, notes, and snippets.

@ugexe
Last active April 3, 2019 17:04
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 ugexe/707df8eddc1f055fdc2ca8c27fceba48 to your computer and use it in GitHub Desktop.
Save ugexe/707df8eddc1f055fdc2ca8c27fceba48 to your computer and use it in GitHub Desktop.
polyglot perl 5/6 benchmark
my @ARGV = ("123456789");
sub to_int($_) { ("0" and (return Int($_[0])) or (return int($_[0]))) };
sub from_base35 {
my ($state, $result, $dict, $base35) = (1, "", {}, @_[0]);
$dict{$_} = $_ for "1".."9";
$dict{$_} = ord($_) - 55 for "A".."Y";
for (reverse grep &{ sub ($_) { $_ ne "" } }.(), split("", $base35)) {
$result += $state * $dict{$_};
$state *= 35;
};
$result
}
sub to_base35 {
my ($result, $dict, $base10) = ("", {}, @_[0]);
$dict{$_} = $_ for "1".."9";
$dict{ord($_) - 55} = $_ for "A".."Y";
while ($base10 > 0) {
$result = join("", $dict{to_int($base10 % 35)}, $result);
$base10 = to_int($base10 / 35);
}
$result;
}
for (1..100000) {
my $to = to_base35(@ARGV[0]);
my $from = from_base35($to);
}
$ time perl benchmark.pl
real 1m2.839s
user 1m2.693s
sys 0m0.063s
$ time perl6 benchmark.pl
real 1m12.175s
user 1m12.214s
sys 0m0.200s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment