File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Performance and the fly likes Performance difference between different ways of iteration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "Performance difference between different ways of iteration" Watch "Performance difference between different ways of iteration" New topic
Author

Performance difference between different ways of iteration

lokesh sree
Ranch Hand

Joined: Oct 27, 2009
Posts: 99

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,


Lokesh
( SCBCD 5, CCENT, SCJP 5 )
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18115
    
    8

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

Joined: Feb 17, 2011
Posts: 2

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


~Buchi Reddy
“Computers are like bikinis. They save people a lot of guesswork.”
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12675
    
    5
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


Java Resources at www.wbrogden.com
lokesh sree
Ranch Hand

Joined: Oct 27, 2009
Posts: 99


@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

Joined: Mar 05, 2008
Posts: 2969
    
    9
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

Joined: Dec 10, 2007
Posts: 23
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Performance difference between different ways of iteration
 
Similar Threads
Performance by Loop optimisation -??
where do you declare local variables ??
Revere elements
Variable declaration
ArrayList