小原です。
http://ruby.gfd-dennou.org/tutorial/ruby-ext/
を参考に作業をした報告です。
私の環境が上記ホームページと異なるためか、すんなりいかないところも
ありました。
(os=vine linux 3.0 )
( ruby 1.8.2 (2004-11-03) [i386-linux])
作業をする中で、いくつかの疑問もでてきました。これらは、thread
を改めて、ご教授を仰ぎたいと思います。
ライフゲームは、とてもできそうにないので、素数を取り出す処理で
時間を計測することにしました。
http://narray.rubyforge.org/index.html.ja
からNArrayをダウンロードして、インストールの途中ですが、
ここにライフゲームの Ruby script がありました。とても
短くて、びっくりしました。私には、解読できなくて、少し
落ち込んでいるところです。
以下、今までの作業です。
-------------------------------------------------
●0 directry foo を作る。(作業はここでする)
●1 swig をインストールする。
(vine の synaptic を使用でラクチン)
●2 prm.c を作る。
(整数xが素数なら return 1)
/*** prm.c ここから ***********************/
/*#include <stdio.h>
#include <math.h>
*/
int prm(int x){
int a,b,c,i;
double max;
if(x<2) return 0;
if(x==2 || x==3) return 1;
if(x%2==0) return 2;
max=sqrt(x);
i=1;
while(i<max){
i+=2;
if(x%i==0) return i;
}
return 1;
}
/*** prm.c ここまで **************************/
●3 forswig.i を作る。
/*******forswig.i ここから*********************/
%module prime
%{
%}
extern int prm(int x);
/*******forswig.i ここまで*********************/
●4 コマンド swig -ruby forswig.i を投入する。
●5 コマンド ruby extconf.rb を投入する。
/********extconf.rb ここから*******************/
require 'mkmf'
create_makefile('prime')
/********extconf.rb ここまで*******************/
●6 コマンド make を投入する。
●7 コマンド ls を投入して、file を確認する。
(prime.so が 生成されている!)
/********確認画面 ここから**********************/
[ohr@localhost foo]$ ls
Makefile forswig.i forswig_wrap.c prime.so* prm.c~
extconf.rb forswig.i~ forswig_wrap.o prm.c prm.o
/********確認画面 ここまで**********************/
●8 script tst.rb を作って実験する。
/**********tst.rb code ここから****************/
#!/usr/bin/env ruby
require 'prime'
def rprm(x)
return 1 if x==2 || x==3
return 0 if x<2
return 2 if x%2==0
i=1;max=Math::sqrt(x+0.5)
while i<max
i+=2
return i if x%i==0
end
return 1
end
loop {
system ("clear")
puts "\ncompare time to calcurate"
puts " pick up 30 primes greater than indicated."
puts " c vis ruby \n "
print " input integer x= "
xx=x=gets.chomp.to_i
break if x==0
t0=Time.now ######## by C
n=0
while n<30
if Prime.prm(x)==1;printf(" %10d",x);n+=1
puts if n%5==0;end
x+=1
end
t1=Time.now
printf("Time by C = %f\n",t1-t0)
t0=Time.now ######## by ruby
n=0;x=xx
while n<30
if rprm(x)==1;printf(" %10d",x);n+=1
puts if n%5==0;end
x+=1
end
t1=Time.now
printf("Time by Ruby = %f\n",t1-t0)
print "OK? : ";e=gets;break if e.chop=="e"
}
/**********tst.rb ここまで********************/
/**********実行画面 ここから********************/
compare time to calcurate
pick up 30 primes greater than indicated.
c vis ruby
input integer x= 1000000
1000003 1000033 1000037 1000039 1000081
1000099 1000117 1000121 1000133 1000151
1000159 1000171 1000183 1000187 1000193
1000199 1000211 1000213 1000231 1000249
1000253 1000273 1000289 1000291 1000303
1000313 1000333 1000357 1000367 1000381
Time by C = 0.001291
1000003 1000033 1000037 1000039 1000081
1000099 1000117 1000121 1000133 1000151
1000159 1000171 1000183 1000187 1000193
1000199 1000211 1000213 1000231 1000249
1000253 1000273 1000289 1000291 1000303
1000313 1000333 1000357 1000367 1000381
Time by Ruby = 0.057976
OK? :
/**********実行画面 ここまで********************/