Skip to content

Instantly share code, notes, and snippets.

@ppazos
Last active March 28, 2024 00:15
Show Gist options
  • Save ppazos/24ff5458f48771aba4d5ef34a28f15bc to your computer and use it in GitHub Desktop.
Save ppazos/24ff5458f48771aba4d5ef34a28f15bc to your computer and use it in GitHub Desktop.
def lines = (1..10000000)
println lines.getClass()
// with list.each {}
def start = System.nanoTime()
lines.each { line->
line++;
}
println ((System.nanoTime() - start) / 1_000_000_000)
// with loop over list
start = System.nanoTime()
for (line in lines){
line++;
}
println ((System.nanoTime() - start) / 1_000_000_000)
// fastest loop
start = System.nanoTime()
for (int line: lines){
line++;
}
println ((System.nanoTime() - start) / 1_000_000_000)
start = System.nanoTime()
lines.forEach (line -> {
line++;
})
println ((System.nanoTime() - start) / 1_000_000_000)
start = System.nanoTime()
for (int i=0; i<lines.size(); i++){
lines.get(i)++;
}
println ((System.nanoTime() - start) / 1_000_000_000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment