2

So I have an arrayList of msgs, each is classified as either static or dynamic
I create a vessel or find a vessel object (from an arrayList of vessels) and add the information to the vessel depending on msg type.

 VesselS vessels = new VesselS(); 
        msgs.forEach(msg -> { 
            if (msg instanceof DynamicDataReport) {
                DynamicDataReport dynamicDataReport = (DynamicDataReport) msg;
                Vessel vessel = new Vessel(msg.getSourceMmsi().getMMSI());
                Coordinate coord = new Coordinate(dynamicDataReport.getLongitude(),dynamicDataReport.getLatitude()); 
                int index = vessels.index(vessel); 
                if(index < 0) { //if vessel is not present in vessels
                    vessel.addCoor(coord);
                    vessels.addVessel(vessel);
                }
                else{ //if vessel is already in vessels 
                    vessels.getVessels().get(index).addCoor(coord);
                }
            }

This is the first part, I thought using an index variable might be easier than searching for the index twice (in the next section of code), but it didn't seem to effect the run time.

else if (msg instanceof StaticDataReport) {
                StaticDataReport staticDataReport = (StaticDataReport) msg;
                Vessel vessel = new Vessel(msg.getSourceMmsi().getMMSI());
                String shipName = staticDataReport.getShipName(); 
                String callSign = staticDataReport.getCallsign(); 
                if(!vessels.contains(vessel)) { //if vessel is not present in vessels
                    vessel.addname(shipName);
                    vessel.setCallSign(callSign);
                    vessels.addVessel(vessel);
                }
                else{ //if vessel is already in vessels 
                    vessels.getVessels().get(vessels.index(vessel)).addname(shipName);
                }
            }
            });

First time working with a lot of information and I need to minimize runtime. Would it be worth looking into storing vessels in trees opposed to lists ? is there redundancy in my code that I can cut down on ?
My contains and index methods are just loops.

public boolean contains(Vessel vessel){ 
    for(int i= 0; i<Vessels.size(); i++){
        if(Vessels.get(i).getMMSI() == vessel.getMMSI()){
            return true; 
        }
    }
    return false; 
}
12
  • What is your equals method in Vessels? Commented May 26, 2015 at 13:09
  • 1
    @sharonbn contains method is in the vessels class that has an arraylist of vessels. Would using new for loop be more efficient ?, I also explained that I tried index in the first part thinking it might effect run time(opposed to using contains). Commented May 26, 2015 at 13:20
  • 1
    @aksappy the hope is to read as many as possible, currently my data set is 1.3 million and it takes 12 seconds total, 6 seconds decoding and 6 seconds running this loop. Commented May 26, 2015 at 13:33
  • 1
    @aksappy yeah, a vessel is comprised of a mmsi, callsign, ArrayList <String> names, ArrayList <String> AIS and ArrayList <Coordinate> coordinates Commented May 26, 2015 at 13:44
  • 2
    If it is unique, you can remove the lengthy contains and index methods by a map, cant you? Commented May 26, 2015 at 13:47

1 Answer 1

2

If your list is modifiable during program execution and if your MMSI code is unique, use a HashMap (faster) or a TreeHashMap with key as MMSI and value as Vessel. Then instead of contains method, use something like below

Vessel vessel = map.get(tempVesse.getMMSI());
if(null!=vessel)
    //do this
else
    // do that

You can use the map.contains() in treehashmap which is using binary search. Cons for doing this - you have to manage a map instead of a list

Or else, you can use Collections.binarySearch directly to search for the required object, which is ought to be faster in theory at least (see an example here). You will have to sort the object list before doing the search operation and that will be an overhead atleast for once, which is the con for this approach

I have not seen any visible impacts on performance from your code otherwise.

And it also includes your design level decision to do an in memory search instead of a DB or something of that sort. A list of 1.3 million objects is a bit heavy, if you ask me.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.