Skip to content

Instantly share code, notes, and snippets.

@notyy
Last active August 29, 2015 14:13
Show Gist options
  • Save notyy/38b63bef751ed306d6a5 to your computer and use it in GitHub Desktop.
Save notyy/38b63bef751ed306d6a5 to your computer and use it in GitHub Desktop.
trying to figure out why foreach is slow
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