sh 与fibonacci(40)的基本性能比较ruby / mruby(解释,mrb字节码,c字节码) Posted 2021-05-07
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh 与fibonacci(40)的基本性能比较ruby / mruby(解释,mrb字节码,c字节码)相关的知识,希望对你有一定的参考价值。
#include <stdint.h>
const uint8_t fib_symbol[] = {
0x52,0x49,0x54,0x45,0x30,0x30,0x30,0x32,0xb2,0xe1,0x00,0x00,0x01,0x52,0x4d,0x41,
0x54,0x5a,0x30,0x30,0x30,0x30,0x49,0x52,0x45,0x50,0x00,0x00,0x01,0x34,0x30,0x30,
0x30,0x30,0x00,0x00,0x00,0xbb,0x00,0x03,0x00,0x07,0x00,0x01,0x00,0x00,0x00,0x1a,
0x01,0x80,0x00,0x48,0x02,0x00,0x00,0xc0,0x01,0x80,0x00,0x46,0x01,0x80,0x00,0x91,
0x01,0x80,0x80,0x20,0x00,0x80,0xc0,0x01,0x01,0x80,0x00,0x06,0x02,0x00,0x00,0x3d,
0x02,0x80,0x00,0x06,0x03,0x40,0x13,0x83,0x02,0x80,0x00,0xa0,0x02,0x01,0x40,0x3e,
0x01,0x80,0xc0,0xa0,0x01,0x80,0x00,0x91,0x01,0x80,0x80,0x20,0x02,0x00,0x40,0x01,
0x01,0x81,0x00,0xae,0x01,0x00,0xc0,0x01,0x01,0x80,0x00,0x06,0x02,0x00,0x01,0x3d,
0x02,0x80,0x80,0x01,0x02,0x01,0x40,0x3e,0x02,0x80,0x01,0xbd,0x02,0x01,0x40,0x3e,
0x01,0x80,0xc0,0xa0,0x00,0x00,0x00,0x4a,0x00,0x00,0x00,0x04,0x00,0x00,0x09,0x46,
0x69,0x62,0x28,0x34,0x30,0x29,0x3d,0x20,0x00,0x00,0x00,0x00,0x00,0x09,0x20,0x20,
0x54,0x69,0x6d,0x65,0x20,0x3d,0x20,0x00,0x00,0x01,0x73,0x00,0x00,0x00,0x05,0x00,
0x03,0x66,0x69,0x62,0x00,0x00,0x04,0x54,0x69,0x6d,0x65,0x00,0x00,0x03,0x6e,0x6f,
0x77,0x00,0x00,0x04,0x70,0x75,0x74,0x73,0x00,0x00,0x01,0x2d,0x00,0x00,0x00,0x00,
0x6d,0x00,0x03,0x00,0x07,0x00,0x00,0x00,0x00,0x00,0x11,0x02,0x00,0x00,0x26,0x01,
0x80,0x40,0x01,0x02,0x40,0x00,0x03,0x01,0x80,0x00,0xb4,0x01,0xc0,0x01,0x19,0x01,
0xc0,0x00,0x03,0x00,0x40,0x04,0x97,0x01,0x80,0x00,0x06,0x02,0x00,0x40,0x01,0x02,
0x00,0x80,0xaf,0x01,0x80,0x40,0xa0,0x02,0x00,0x00,0x06,0x02,0x80,0x40,0x01,0x02,
0x80,0x81,0x2f,0x02,0x00,0x40,0xa0,0x01,0x80,0xc0,0xac,0x01,0x80,0x00,0x29,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x00,0x02,0x3c,0x3d,0x00,0x00,0x03,0x66,0x69,
0x62,0x00,0x00,0x01,0x2d,0x00,0x00,0x01,0x2b,0x00,0x45,0x4e,0x44,0x00,0x00,0x00,
0x00,0x08,
};
def fib(n)
if n <= 1
1
else
fib(n - 1) + fib(n - 2)
end
end
t0 = Time.now
puts "Fib(40)= #{ fib(40) }"
t = Time.now - t0
puts " Time = #{ t }s"
#include "mruby.h"
#include "mruby/irep.h"
#include "fib.c"
int main(void)
{
mrb_state *mrb = mrb_open();
if (!mrb) { /* handle error */ }
mrb_load_irep(mrb, fib_symbol);
mrb_close(mrb);
}
#include<stdio.h>
#include<time.h>
int fib(int n){
if (n <= 1){
return 1;
} else {
return fib(n -1)+ fib(n - 2);
}
}
int main()
{
clock_t t0 = clock();
long f = fib(40);
clock_t t1 = clock();
double te = ((double)(t1 - t0)) / CLOCKS_PER_SEC;
printf("Fib(40)=%d, time=%f\n", f, te);
return 0;
}
#include<stdio.h>
#include<time.h>
int fib(int n){
int first = 0, second = 1, next, c;
for ( c = 0 ; c < n ; c++ )
{
if ( c <= 1 ) {
next = c;
} else {
next = first + second;
first = second;
second = next;
}
}
return next;
}
int main()
{
clock_t t0 = clock();
long f = fib(40);
clock_t t1 = clock();
double te = ((double)(t1 - t0)) / CLOCKS_PER_SEC;
printf("Fib(40)=%d, time=%f\n", f, te);
return 0;
}
fer@hecate /tmp/mruby [ruby-2.3.1]
$ ruby fib.rb
Fib(40)= 165580141
Time = 18.012413533s
fer@hecate /tmp/mruby [ruby-2.3.1]
$ rvm use mruby
Using /home/fer/.rvm/gems/mruby-1.0.0
Warning! Executable 'gem' missing, something went wrong with this ruby installation!
fer@hecate /tmp/mruby [mruby-1.0.0]
$ mruby fib.rb
Fib(40)= 165580141
Time = 33.551006s
fer@hecate /tmp/mruby [mruby-1.0.0]
$ mrbc fib.rb
fer@hecate /tmp/mruby [mruby-1.0.0]
$ mruby -b fib.mrb
Fib(40)= 165580141
Time = 32.033073s
fer@hecate /tmp/mruby [mruby-1.0.0]
$ mrbc -Bfib_symbol fib.rb
fer@hecate /tmp/mruby [mruby-1.0.0]
$ ls
fib.c fib.mrb fib.rb test_program test_stub.c
fer@hecate /tmp/mruby [mruby-1.0.0]
$ vi fib_main.c
fer@hecate /tmp/mruby [mruby-1.0.0]
$ gcc -std=c99 -I/home/fer/.rvm/rubies/mruby-1.0.0/include fib_main.c -o fib.exe -lm /home/fer/.rvm/src/mruby-1.0.0/build/host/lib/libmruby.a
fer@hecate /tmp/mruby [mruby-1.0.0]
$ ls
fib.c fib.exe fib_main.c fib.mrb fib.rb
fer@hecate /tmp/mruby [mruby-1.0.0]
$ ./fib.exe
Fib(40)= 165580141
Time = 30.911468s
fer@hecate /tmp/mruby [mruby-1.0.0]
$ vi fib_native.c
fer@hecate /tmp/mruby [mruby-1.0.0]
$ gcc -std=c99 fib_native.c -o fib_native.exe
fer@hecate /tmp/mruby [mruby-1.0.0]
$ ./fib_native.exe
Fib(40)=165580141, time=0.971636
fer@hecate /tmp/mruby [mruby-1.0.0]
$ vi fib_native_for.c
fer@hecate /tmp/mruby [mruby-1.0.0]
$ gcc -std=c99 fib_native_for.c -o fib_native_for.exe
fer@hecate /tmp/mruby [mruby-1.0.0]
$ ./fib_native_for.exe
Fib(40)=63245986, time=0.000002
以上是关于sh 与fibonacci(40)的基本性能比较ruby / mruby(解释,mrb字节码,c字节码)的主要内容,如果未能解决你的问题,请参考以下文章
for循环和R中的函数
sh ShellScript中的Fibonacci
Fibonacci数列
Fibonacci数列
入门训练——Fibonacci数列
入门训练 Fibonacci数列