Last active
December 18, 2015 01:29
-
-
Save zhuomingliang/5704174 to your computer and use it in GitHub Desktop.
fib(28) benchmarks between Parrot, Perl5 and MoarVM
This file contains hidden or 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
| $ parrot fib.pir # Parrot | |
| fib(28) = 317811 | |
| start : 1370329827.014 | |
| end : 1370329829.229 | |
| end - start: 2.21500015258789 | |
| $ perl fib.pl # Perl 5 | |
| fib(28) = 317811 | |
| start : 1370329768.826 | |
| end : 1370329769.45 | |
| end - start: 0.624000072479248 | |
| $ nqp bench/fib.t # MoarVM | |
| fib(28) = 317811 | |
| start : 1370329844.736027 | |
| end : 1370329845.001228 | |
| end - start: 0.265201 |
This file contains hidden or 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
| .sub main :main | |
| .param pmc argv | |
| .local int argc | |
| argc = argv | |
| .local int N | |
| N = 28 | |
| if argc <= 1 goto noarg | |
| N = argv[1] | |
| noarg: | |
| .local int r | |
| $N1 = time | |
| r = fib(N) | |
| $N2 = time | |
| say $N1 | |
| say $N2 | |
| $N2 = $N2 - $N1 | |
| say $N2 | |
| print "fib(" | |
| print N | |
| print ") = " | |
| say r | |
| .end | |
| .sub fib | |
| .param int n | |
| if n < 2 goto ret | |
| .local int n1, n2, r1, r2 | |
| n1 = n - 1 | |
| n2 = n - 2 | |
| r1 = fib(n1) | |
| r2 = fib(n2) | |
| n = r1 + r2 | |
| ret: | |
| .return (n) | |
| .end |
This file contains hidden or 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
| use strict; | |
| use warnings; | |
| use Time::HiRes qw(time); | |
| sub fib { | |
| my $n = shift; | |
| return $n if ( $n < 2 ); | |
| return fib( $n - 1 ) + fib( $n - 2 ); | |
| } | |
| my $N = shift || 28; | |
| my $M = time; | |
| print "fib($N) = ", fib($N), "\n"; | |
| my $O = time; | |
| print $M, "\n"; | |
| print $O, "\n"; | |
| print time - $M; |
This file contains hidden or 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
| #!nqp | |
| use MASTTesting; | |
| plan(1); | |
| sub fibsub() { | |
| my $frame := MAST::Frame.new(); | |
| my $r0 := local($frame, int); | |
| my $r1 := local($frame, int); | |
| my $r2 := local($frame, int); | |
| my $r3 := local($frame, NQPMu); | |
| my $two := const($frame, ival(2)); | |
| my @ins := $frame.instructions; | |
| my $skip_return := label('1'); | |
| op(@ins, 'checkarity', ival(1), ival(1)); | |
| op(@ins, 'param_rp_i', $r0, ival(0)); | |
| op(@ins, 'lt_i', $r1, $r0, $two); | |
| op(@ins, 'unless_i', $r1, $skip_return); | |
| op(@ins, 'return_i', $r0); | |
| nqp::push(@ins, $skip_return); | |
| op(@ins, 'set', $r2, $r0); | |
| op(@ins, 'set', $r1, $r0); | |
| op(@ins, 'dec_i', $r2); | |
| op(@ins, 'dec_i', $r1); | |
| op(@ins, 'dec_i', $r1); | |
| op(@ins, 'getcode', $r3, $frame); | |
| call(@ins, $r3, [$Arg::int], $r1, :result($r1)); | |
| call(@ins, $r3, [$Arg::int], $r2, :result($r2)); | |
| op(@ins, 'add_i', $r2, $r1, $r2); | |
| op(@ins, 'return_i', $r2); | |
| return $frame; | |
| } | |
| sub runfib($n) { | |
| mast_frame_output_is(-> $frame, @ins, $cu { | |
| my $fibsub := fibsub(); | |
| my $r0 := const($frame, ival($n)); | |
| my $r1 := local($frame, num); | |
| my $r2 := local($frame, num); | |
| my $r3 := local($frame, NQPMu); | |
| op(@ins, 'time_n', $r1); | |
| op(@ins, 'getcode', $r3, $fibsub); | |
| call(@ins, $r3, [$Arg::int], $r0, :result($r0)); | |
| op(@ins, 'time_n', $r2); | |
| op(@ins, 'say_n', $r1); | |
| op(@ins, 'say_n', $r2); | |
| op(@ins, 'sub_n', $r2, $r2, $r1); | |
| op(@ins, 'say_n', $r2); | |
| op(@ins, 'say_i', $r0); | |
| op(@ins, 'return'); | |
| $cu.add_frame($fibsub); | |
| }, | |
| "", | |
| "recursive fi $n", 1); | |
| } | |
| runfib(28); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment