LoginSignup
8
1

More than 5 years have passed since last update.

ScalaNativeにフィボナッチ解かせてみた

Last updated at Posted at 2017-03-15

祝・ScalaNative0.1リリース、ということでフィボナッチを解かせてみた。

http://qiita.com/akira_/items/55cf1f5911b14e265c6f
これのJSコードを移植。

object Main {
  def main(args: Array[String]): Unit = {
    println("Scala Native")
    println(s"fibonacci(46) = ${fibonacci(46)}")
  }
  def fibonacci(n: Long): Long = {
    if (n <= 1) {
      1
    } else {
      fibonacci(n - 1) + fibonacci(n - 2)
    }
  }
}

System.currentTimeMillis()は使えなかった、のでtimeコマンドで計測してみた

環境は↓

CPU:       Quad core Intel Core i7-4870HQ (-HT-MCP-) cache: 6144 KB 
           clock speeds: max: 3700 MHz 1: 3247 MHz 2: 3234 MHz 3: 3268 MHz 4: 3214 MHz 5: 3224 MHz 6: 3251 MHz
           7: 3226 MHz 8: 3270 MHz

そして結果↓

 % time ./target/scala-2.11/scala-native-example-out
Scala Native
fibonacci(46) = 2971215073
./target/scala-2.11/scala-native-example-out  6.48s user 0.00s system 99% cpu 6.480 total

はい

-----追記
上記の結果はビルドオプション( http://www.scala-native.org/en/latest/user/sbt.html#sbt-settings )である nativeModerelease のもの。
debug だとかなり遅い。デフォルトだと debug モード。
( sbt "set nativeMode := \"release\"" nativeLink とかで切り替えられる。)

debug モードでの結果↓

 % time ./target/scala-2.11/scala-native-example-out
Scala Native
fibonacci(46) = 2971215073
./target/scala-2.11/scala-native-example-out  14.77s user 0.00s system 99% cpu 14.774 total
8
1
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
8
1