🔙 Back to Top

Mar 06 2014

go の実験

19:03:47 test$ time gosh test.scm
354224848179261915075

real    0m0.027s
user    0m0.008s
sys     0m0.008s
19:04:02 test$ time gosh test.scm
354224848179261915075

real    0m0.029s
user    0m0.008s
sys     0m0.012s
19:04:05 test$ time runghc test.hs
354224848179261915075

real    0m0.189s
user    0m0.160s
sys     0m0.030s
19:04:13 test$ time runghc test.hs
354224848179261915075

real    0m0.188s
user    0m0.172s
sys     0m0.018s
19:04:15 test$ time go run test.go
3736710778780434371

real    0m0.171s
user    0m0.146s
sys     0m0.027s
19:04:20 test$ time go run test.go
3736710778780434371

real    0m0.159s
user    0m0.136s
sys     0m0.025s

Goとかいう動的型付け言語だけ答えが違うのはint64でやっちゃったから

(define (fib a b n)
  (if (zero? n) a (fib b (+ a b) (- n 1))))

(display (fib 0 1 100))
import Data.List
import Data.Char
import Data.Maybe
import Data.Tuple
import Debug.Trace
import Data.Array
import Data.Array.IO
import Control.Monad
import Control.Applicative
import Control.Arrow
import qualified Data.ByteString.Char8 as B
import qualified Data.Map as M
import qualified Data.Set as S
import Text.Printf

readInt :: B.ByteString -> Int
readInt = fst . fromJust . B.readInt

readInt2 :: B.ByteString -> (Int, Int)
readInt2 = l2t . map readInt . B.words
readInt3 :: B.ByteString -> ((Int,Int),Int)
readInt3 = l3t . map readInt . B.words

l2t [x,y] = (x,y)
l3t [x,y,z] = ((x-1,y-1),z)

(|>) x f = f x; infixl 1 |>
car = head
cdr = tail
cadr = car . cdr
cddr = cdr . cdr
caddr = car . cddr

type Cost = M.Map (Int, Int) Int

main :: IO ()
main = do
    let fibs = iterate (\(a,b) -> (b,a+b)) (0, 1)
    print $ fst $ fibs !! 100
package main

import (
   "fmt"
 )

func main() {
  var x, y int64
  x = 0
  y = 1
  for i := 0; i < 100; i++ {
    x, y = y, x+y
  }
  fmt.Println(x)
}

てきとーにgolang 多倍長でググったらOKIのページが見つかった。

"An experimental package in Go for mixed mode arithmetic including rational numbers


19:13:57 test$ time runghc test.hs
354224848179261915075

real    0m0.130s
user    0m0.105s
sys     0m0.026s
19:13:58 test$ time runghc test.hs
354224848179261915075

real    0m0.128s
user    0m0.108s
sys     0m0.022s

1.44倍速になった!

main :: IO ()
main = do
    let fibs = iterate (\(a,b) -> (b,a+b)) (0, 1)
    print $ fst $ fibs !! 100