Created
December 30, 2014 12:08
-
-
Save iszlai/52da08584a6977a639be to your computer and use it in GitHub Desktop.
Scala functional vs imperative
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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