Last active
August 29, 2015 14:13
-
-
Save notyy/38b63bef751ed306d6a5 to your computer and use it in GitHub Desktop.
trying to figure out why foreach is slow
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
import java.util.Date | |
object PerfOfWhile extends App { | |
def whileLoop(size: Int): Int = { | |
var c = 0 | |
while(c < size){ | |
c += 1 | |
} | |
c | |
} | |
def foreachLoop(size: Int): Int = { | |
var c = 0 | |
(1 to size).foreach(_ => c += 1) | |
c | |
} | |
val size = 10000000 | |
var start = new Date().getTime | |
println(s"while loop rs: ${whileLoop(size)}") | |
var end = new Date().getTime | |
println(s"time used: ${end - start}") | |
start = new Date().getTime | |
println(s"foreach loop rs: ${foreachLoop(size)}") | |
end = new Date().getTime | |
println(s"time used: ${end - start}") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment