Skip to content

Instantly share code, notes, and snippets.

@gnufied
Created August 15, 2009 04:38
Show Gist options
  • Save gnufied/168266 to your computer and use it in GitHub Desktop.
Save gnufied/168266 to your computer and use it in GitHub Desktop.
import java.util.ArrayList
import java.util.LinkedList
import java.util.List
trait Benchmark {
def run(func: => Unit) = {
val startedAt = System.currentTimeMillis
func
val stoppedAt = System.currentTimeMillis
println("Time taken is :" + (stoppedAt - startedAt))
}
}
object App extends Benchmark {
val count = 2000000
def main(args: Array[String]) = {
println("What follows involves JVM warmup and hence ignore")
run {
iterateUsingArrayList
}
run {
iterateUsingLinkedList
}
println("Running first iteration is done")
Thread.sleep(2*400)
run {
iterateUsingArrayList
}
run {
iterateUsingLinkedList
}
println("Running second iteration done")
Thread.sleep(10*1000)
}
def iterateUsingArrayList = {
val a: List[String] = new ArrayList()
for(x <- 0 to count) a.add("hello " + x)
val iterator = a.iterator()
while(iterator.hasNext()) {
iterator.next()
}
}
def iterateUsingLinkedList = {
val a: List[String] = new LinkedList()
for(x <- 0 to count) a.add("hello " + x)
val iterator = a.iterator()
while(iterator.hasNext()) {
iterator.next()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment