Skip to content

Instantly share code, notes, and snippets.

@iszlai
Created December 30, 2014 12:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iszlai/52da08584a6977a639be to your computer and use it in GitHub Desktop.
Save iszlai/52da08584a6977a639be to your computer and use it in GitHub Desktop.
Scala functional vs imperative
object Dot {
def main(args: Array[String]) {
println("Imperative")
val startTime = System.nanoTime()
testImperative()
val endTime = System.nanoTime()
println((endTime - startTime) / 1000000 + "\n")
println("Functional")
val startTime3 = System.nanoTime()
testFunctional()
val endTime3 = System.nanoTime()
println((endTime3 - startTime3) / 1000000 + "\n")
}
def testFunctional() {
val f = Array(1, 2, 3)
val s = Array(1.0, 2.0, 3.0)
val res = SMoriczka.dotProduct(s, f)
println(res)
}
def testImperative() {
val f = Array(1, 2, 3)
val s = Array(1.0, 2.0, 3.0)
val res = SMoriczka.dotProduct2(s, f)
println(res)
}
}
Output:
Imperative
14.0
23
Functional
14.0
93
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment