Created
August 15, 2009 04:38
-
-
Save gnufied/168266 to your computer and use it in GitHub Desktop.
This file contains hidden or 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.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