Iterating has bad performance after sort.
Here is my code
method() {
var time = System.currentTimeMillis()
arraylist = ArrayList<Float>()
for(i in 1..1000000){
arraylist.add(Math.random().toFloat())
}
Log.i("TIME", (System.currentTimeMillis() - time).toString())
for(i in 1..5){
time = System.currentTimeMillis()
for(a in arraylist){
//nothing
}
Log.i("TIME To Iterate", (System.currentTimeMillis() - time).toString())
}
arraylist.sort()
for(i in 1..5){
time = System.currentTimeMillis()
for(a in arraylist){
//nothing
}
Log.i("TIME To Iterate", (System.currentTimeMillis() - time).toString())
}
}
}
Here is a result
Result said iterating performance be lower after sorting
Why iterating performance is lowered when sorting?