LoginSignup
2
0

More than 3 years have passed since last update.

Kotlin vs Go 処理時間を比較してみる

Posted at

1. はじめ

最近はどこもかしこもGoばっかり!
SE(笑)で化石のようなJavaを扱った経験を持つ人間としてはKotlinの方がしっくりくるのですが、なんだか肩身が狭いです。

そこで今回はGoとKotlinで同じようなコードを書いてみて処理時間という点でどれだけ差があるか実験してみました。

2. 内容

・Kotlinはコンパイラでjarにした後、Amazon Corretto8のランタイムで動かす
・Goは公式でリリースされてる最新のものを使用(Go 1.14)

1回目

・10万の要素を持つ配列に乱数で値をひたすら入れてみる
・とりあえずこれだけ

main.kt
import kotlin.random.Random

fun main(args: Array<String>) {

    val startTime = System.currentTimeMillis()

    var listData: Array<Int> = Array(100000){it}
    for (i in listData) {
        listData[i] = Random.nextInt(100000) + 100
    }

    /*
    listData.forEach {
        println("it:" + it)
    }
    */

    val endTime = System.currentTimeMillis()
    println("開始時刻:" + startTime + " ms")
    println("終了時刻:" + endTime + " ms")
    println("処理時間:" + (endTime - startTime) + " ms")
    println("listData要素数:" + listData.size)
}
main.go
package main

import (
    "fmt"
    "time"
    "math/rand"
)

func main() {

  startTime := time.Now()

  listData := [100000] int{}
  for index := range listData {
    listData[index] = rand.Intn(100000) + 100
  }

  /*
  for _, value := range listData {
    fmt.Printf("it:%d \n" , value)
  }
  */

  endTime := time.Now()
  fmt.Printf("開始時刻:%v \n" , startTime)
  fmt.Printf("終了時刻:%v \n" , endTime)
  fmt.Printf("処理時間:%v \n", (endTime.Sub(startTime)))
  fmt.Printf("listData要素数:%d \n" , len(listData))
}

【結果】

Kotlin:19ms
Go :1.0418ms
main.kt - 20200301 - 1発目.png
main.go - 20200301 - 1発目.png

2回目

・consoleに配列の内容を全出力というものを足すとどうだ??
.kt、.goともに前述のコードのコメント部分復活

    //.kt
    listData.forEach {
        println("it:" + it)
    }

    //.go
    for _, value := range listData {
        fmt.Printf("it:%d \n" , value)
    }

【結果】

Kotlin:19.76s
Go :17.41s
main.kt - 20200301 - 2発目.png
main.go - 20200301 - 2発目.png

おわり

上記の実験ではどちらもGoが圧勝する内容でした。
単純なロジックだと勝負にならないくらいの差ですね。

2
0
2

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
2
0