LoginSignup
0
0

More than 5 years have passed since last update.

学習日誌 第二項

Posted at

FizzBuzz を利用してFortranとpythonの差を計ってみた。
なおきちんとCPUやらメモリーやらを指定してはいないので、お粗末なデータとなってます。

Fortran90

FizzBuzz.f90
implicit none
integer i,j
integer t1, t2, t_rate, t_max, diff
call system_clock(t1)

  read(*,*) j
  do i=1,j
   if (mod(i,15)==0) then
      write(*,*)"FizzBuzz"
   else if (mod(i,3)==0) then
      write(*,*)"Fizz"
   else if (mod(i,5)==0) then
      write(*,*)"Buzz"
   else
      write(*,*) i
   end if
  end do

call system_clock(t2, t_rate, t_max)
  if ( t2 < t1 ) then
    diff = (t_max - t1) + t2 + 1
  else
    diff = t2 - t1
  end if
print "(A, F10.3)", "time it took was:", diff/dble(t_rate)
end

python3.5

FizzBuzz.py
import time
n=input()
n=int(n)
start = time.time()

for i in range(0,n+1):
    if (i%15)==0 :
        print("FizzBuzz")
    elif (i%3)==0 :
        print("Fizz")
    elif (i%5)==0 :
        print("Buzz")
    else:
        print(i)

elapsed_time = time.time() - start
print ("elapsed_time:{0}".format(elapsed_time) + "[sec]")


結果

j の値 Fortran 実行時間[sec] python 実行時間[sec]
100 1.281 0.002621
5000 3.704 0.1030
9999 1.968 0.2104
100000000 1591 1847



数値のブレはちょこちょこあるものの、大体こんな感じ。
1億は試行回数一回のお粗末なので、結構なブレがありそうですが、見事に逆転しました。
ビッグデータの処理ならコンパイル型の方が早いという証左かもしれない。
使い分けが一番大事ってはっきりわかんだね。
今の所言えるのは、Fortranは計算が早いけど、分類とかを早くしようとするなら少し工夫がいると思う。
そもそも一回一回表記させてるのも問題だとは思う。

以上!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0