• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Help to optimize the Nested For Loop logic  RSS feed

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

I have a nested for loop in my code which has 60,000 numObjects in each for loop.

So below numOfObjects = 60,000



The above nested for loop code takes around 10 mins to execute - How can I optimize this code ? Quick help much appreciated - thanks in advance !



Thanks
-AP
 
Bartender
Posts: 3321
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the ranch.

Please post code in code tags so it displays properly.Ignore this line this seems to have been fixed already.

Before trying to optimise anything you need to ascertain what section/line of code needs optimising and the only way I know of doing that is to use a profiler.
BTW Profilers are freely available in Netbeans and Eclipse.

Once you have identified where the bottleneck is we may then be able to advise on alternative ways of writing the code.

Having said all of that Vectors methods are synchronized so unless the collection will be accessed by multiple threads you will get a small speed improvement by changing to an ArrayList but I doubt it will solve your performance problems.
 
Rancher
Posts: 1020
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The exact type of the objects is not clear, but some Lists are slower to traverse with successive get's than with an iterator.

java.util.ListIterator
 
Bartender
Posts: 4179
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe if you explain in english what those loops are supposed to do, you might find it easier to code. To me it looks like you want to sort the Lists so that all of the parts which have the same 'MasterView' are organized together. If that is the case, I would think that you would get better performance using Collections.sort(List,Comparator) where you have to modify two things:
1) The areMasterViewsEqual() method turns into a compare() method
2) It returns 0 when the views are equal,
3) Has some way of determining when one view should be 'before' another. The determination could be arbitrary, but needs to be consistent (so if compare(part1, part2) returns a positive number, compare(part2, part1) needs to return a negative number).
 
Sheriff
Posts: 23714
50
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My impression was that the goal of the code was to remove duplicate entries from some kind of a list. Which differs significantly from your impression.

But all that means is that we DO need to be told what the code is supposed to be doing.
 
Amit Patole
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

Firstly thanks for your quick response and secondly I agree I could have explained better what the for loops do right at the start of my thread .Anyways I will do that now
The intention of this nested loop is quite close to what Steve's thinking [Steve thanks for the Comparator suggestion] !

The first for loop contains 60,000 part objects which have master and view as its attributes. What I want to do in the 2nd for loop is to compare the 1st.....59,000 parts attributes from the 1st loop in the list with the 2nd,3rd...60,000 parts attributes in the subsequent iterations of the 2nd loop.

So if the 1st...n part's "master and view" references matches with the 2nd....n parts "master and view" references I need to group all such parts together in a Vector /ArrayList which will then be used further for a service [DB ] invocation outside the 2nd for loop [Updated code snippet with this modification].
This nested for loop takes around 12 minutes to execute in my system [windows 7, RAM 16 GB].

So the goal of the nested for loop is to group together similar parts!

I hope that clarifies things !

So can you help me decide which approach would reduce this performance issue ?

Thanks in advance for your help !
-Amit
 
Ivan Jozsef Balazs
Rancher
Posts: 1020
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Also Vector is synchronized and hence slower than, say, ArrayList. Vector predates the Collection API and was retrofitted to implement List. I suggest to rather use ArrayList.
 
Amit Patole
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ivan, All

Thanks ! By replacing Vector to ArrayList saved 3 minutes.

So now the code takes around 7 minutes. Any further help !
Thanks
-Amit
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Amit Patole wrote:Any further help !


See Tony Docherty's first reply
 
Amit Patole
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks All -
I will start on steve's suggestion of using Collections.sort(List,Comparator) to further refine the performance hit.
Please let me know if I am in the incorrect direction !

Regards
-Amit
 
Ivan Jozsef Balazs
Rancher
Posts: 1020
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you could come up with a correct hashCode() function, then a hashmap-based lookup would be faster in revealing the equaling elements.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Amit Patole wrote:I will start on steve's suggestion of using Collections.sort(List,Comparator) to further refine the performance hit.
Please let me know if I am in the incorrect direction !


A slight generalization of Ivan's post:
1. If you can come up with some way of determining whether two of your objects are "equal", you can write an equals() method for it.
2. If you write an equals() method, you should also write a hashCode() method.
3. Can you also decide whether one object is "greater" than another? If such a comparison is meaningless, then it's probably best to simply write a Comparator, as Steve suggested; but if it isn't, then you could also make the object class Comparable (see java.lang.Comparable).

All of the above will help you to do what you want with existing Java classes.

Winston
 
Tony Docherty
Bartender
Posts: 3321
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What class type is notRealParts?
Assuming it's a type of List, if you are doing lots of remove operations on it you may get some performance improvements by using a LinkedList.
 
Ivan Jozsef Balazs
Rancher
Posts: 1020
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A hashcode-based lookup instead of the sequential one would buy much improvement.
 
Amit Patole
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

All you guys - Thanks a lot for your help !
I will try all your individual suggestions and keep you posted on the development !

Regards
-Amit
 
Amit Patole
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tony Docherty wrote:What class type is notRealParts?
Assuming it's a type of List, if you are doing lots of remove operations on it you may get some performance improvements by using a LinkedList.



Hello Tony - Yes notRealParts is of type List ! I will try with LinkedList.

Regards
-Amit
 
Amit Patole
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Guys,

Firstly thanks to you all individually, for supporting me and showing me different ways to eliminate this issue.

At last I was able to write some performing code which now takes only 8 seconds for 60,000 part objects ! Below is the renewed code


Thanks !

Regards
-Amit
 
Tony Docherty
Bartender
Posts: 3321
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You don't need to put the the key value pair back into the map as they are already in the map.
 
Amit Patole
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh Good Catch Tony Thanks I will correct that !
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!