Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Performance difference between different ways of iteration

 
lokesh sree
Ranch Hand
Posts: 100
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

I was trying out different ways of iterating over huge collections and wanted to understand the results. This is not done for any specific use case or practical requirement.


I think i have come across case-2 in "effective java" book.
When I ran this multiple times, the results varied.
Samples:

Iter loop time = 1766
Iter loop time = 1422
Iter loop time = 1484

Iter loop time = 1656
Iter loop time = 1531
Iter loop time = 1453

Iter loop time = 1594
Iter loop time = 1453
Iter loop time = 1312

Iter loop time = 1672
Iter loop time = 1516
Iter loop time = 1562


My questions are :
1. Why is the time taken differing for the same loop in multiple runs? loop1 took 1766, 1656, 1594 etc. what all does it depend on?
2. Why is loop3 taking longer time(surprisingly only sometimes) compared to loop2? Shouldn't case3 be the one executing the fastest of all the above 3?

Appreciate any thoughts or comments on this behaviour.
Thanks,
 
Paul Clapham
Sheriff
Pie
Posts: 20771
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The differences are mostly due to random things happening, I expect.

Note that case 3 is the only case which actually iterates through the list.

Try rerunning the code with the cases in a different order and see what happens.
 
Buchi Reddy Busireddy
Greenhorn
Posts: 2
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Loop 1 and 2 are mostly same. I think the time difference you are noticing is mainly due to JVM's optimization. JVM optimizes your code when the same code path is executed many times.

Coming to Loop#3, this actually creates and iterator internally and itr.next() is invoked to traverse the elements. So, this is not expected to be uber fast anyways.

--
Regards,
Buchi Reddy
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13056
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would bet that the System.out.println() on every iteration consumes practically all of observed CPU time anyway so you are not really measuring iteration.

Bill

 
lokesh sree
Ranch Hand
Posts: 100
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

@Paul,
Completely missed the part that loop3 is the only one iterating through the list. Thanks for pointing to that.
Ok, in that case, i cannot compare it with loop 1 and 2.

@Bill,
Without the print statements inside the loops, I could not even measure the time taken for each loop. The diff1 or diff2 was printing as 0.

Between loop1 and loop2, i.e,
for(int i=0; i<list.size();i++)
And
for(int i=0, j=list.size(); i<j;i++)
will there be any difference? does getting the list.size() into j in loop2 rather than getting it for each iteration in i<list.size in loop 1 save any time?
Agreed that it is very trivial. But wanted to know if it saves at least any micro or nano seconds? or it does not make any difference at all?
 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lokesh chenta wrote:
@Bill,
Without the print statements inside the loops, I could not even measure the time taken for each loop. The diff1 or diff2 was printing as 0.

Which supports what Bill said: the difference in iteration speed (if you were actually iterating) is insignificant compared to the time to print things.
 
Lucas Lech
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey

Your testing method is a bit naive. You should most likely profile each iteration method in isolation for at least few minutes
to reach any conclusions. Actually I did something similar when testing mahout claims they had "fast" collections.
My findings were that the good old simple for was the fastest of the lot. I don't have the results handy but I'll try to find them.

Regards,
Lucas
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic