Thanks Sean! I'll optimize some of my other for(;;) loops as well... On Mon, Jun 15, 2015 at 3:59 PM, Sean Luke <[log in to unmask]> wrote: > MASON actually has a MutableDouble. > > java/mason/sim/util/MutableDouble.java > > You could use that. Also, don't use for(Object object : objects) -- it translates to building an Iterator, and at least as of Java 7 Iterators are still quite slow. > > I'd do this: > > final public void SparseGrid2DMultiply(SparseGrid2D theGrid, double multiplier) { > Bag objects = theGrid.getAllObjects(); > int len = objects.size(); > for(int i = 0; i < len; i++) > { > MutableDouble d = (MutableDouble)(objects.get(i)); > d.val *= multiplier; > } > } > > > On Jun 15, 2015, at 1:02 PM, Sadat Chowdhury <[log in to unmask]> wrote: > >> I still have a lot of files to change, and have not actually run/tested with this new method — maybe it would throw the ConcurrentModificationException, I don’t know yet. >> >> I am going to look into MutableDouble — it makes more sense as far as accuracy goes. Thanks! >> >> >>> On Jun 15, 2015, at 12:58 PM, Ernesto Carrella <[log in to unmask]> wrote: >>> >>> You could try populating your SparseGrid2D with either an AtomicDouble or a MutableDouble from commons-lang. >>> That way you can modify them without pulling them out of the grid and putting them back in. >>> >>> I am quite surprised it doesn't throw a ConcurrentModification exception since you are effectively looping through a list of objects that you are also modifying (with remove() and setLocation()). I suppose MASON passes a safe copy in that bag. >>> >>> On Mon, Jun 15, 2015 at 5:23 PM Sadat Chowdhury <[log in to unmask]> wrote: >>> Hi All, >>> >>> When I increased the 2D world size of my system, there was a significant slowdown of the simulation. After I investigated using a profiler, it turned out to be due to methods that scan through 2D grids that in my system are defined as DoubleGrid2D. >>> >>> I have started converting those DoubleGrid2D into SparseGrid2D — because it fits the description perfectly: the world can be potentially unbounded and most times there will be fewer actual objects in the world. >>> >>> While it was relatively easy to convert all the parts that was setting and accessing objects in the grid — I am at a point where I have a lot of DoubleGrid2D’s that used full-scanning functions like .lowerBound() and .multiply(). Supposing I have a SparseGrid2D that has only one Double object per coordinate, what would be the most efficient (fast) way to implement a function similar to multiply() ? >>> >>> This is what I have so far, but I am just wondering if it can be done more efficiently (faster)? >>> >>> final public void SparseGrid2DMultiply(SparseGrid2D theGrid, >>> double multiplier) { >>> /* >>> * theGrid is assumed to contain one and only one Double in a given >>> * coordinate >>> */ >>> Bag objects = theGrid.getAllObjects(); >>> for (Object object : objects) { >>> double val = (Double) object; >>> if (val != 0.0) { >>> val = val * multiplier; >>> Int2D location = theGrid.getObjectLocation(object); >>> theGrid.remove(object); >>> theGrid.setObjectLocation(val, location); >>> } >>> } >>> >>> } >>> >>> Any thoughts/comments is greatly appreciated, >>> Sadat.