Skip to content

Instantly share code, notes, and snippets.

@hrj
Created February 19, 2015 19:10
Show Gist options
  • Select an option

  • Save hrj/c3e1b4dab12b944e12f7 to your computer and use it in GitHub Desktop.

Select an option

Save hrj/c3e1b4dab12b944e12f7 to your computer and use it in GitHub Desktop.
Scala v/s Kotlin
var count = 0
var totalTime = 0L
fun test() {
val start = System.currentTimeMillis()
val x = (1L..5000000L) map {it * 40} reduce {a,b -> (a*b)%19 + 1}
val end = System.currentTimeMillis()
val timeTaken = (end - start)
totalTime += timeTaken
println("answer is: " + x)
println(" time: " + timeTaken + " ms")
count += 1
}
test()
test()
test()
test()
test()
test()
test()
test()
test()
test()
println("Avg time: " + (totalTime.toDouble() / count))
var count = 0
var totalTime = 0L
def test() {
val start = System.currentTimeMillis()
val x = (1L to 5000000L) map {_ * 40} reduce {(a,b) => (a*b)%19 + 1}
val end = System.currentTimeMillis()
val timeTaken = (end - start)
totalTime += timeTaken
println("answer is: " + x)
println(" time: " + timeTaken + " ms")
count += 1
}
test()
test()
test()
test()
test()
test()
test()
test()
test()
test()
println("Avg time: " + (totalTime.toDouble / count))
@ctongfei
Copy link

In Scala you should do (1 to 1000000).view that turns it lazy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment