2

I have a nested loop in the code below which I'm trying to optimize as I know nested for loop is very expensive, anyone has a different way to achieve this?

Thanks in advance!

private List<Map<String, Object>> updateSomething(List<Map<String, Object>> list)
        throws MetadataException {
    for (Map<String, Object> map : list) {
        setFilePathAndOffsetParams(map);
        for (Map.Entry<String, String> entry : anotherMap.entrySet()) {
            updateKeyOnMap(map, entry.getKey(), entry.getValue());
        }
    }
    return list;
}

private void updateKeyOnMap(Map<String, Object> map, String newKey, String oldKey) {
    if (!newKey.equals(oldKey)) {
        map.put(newKey, map.get(oldKey));
        map.remove(oldKey);
    }
7
  • Use an array of indices. See this link -> stackoverflow.com/questions/18028344/… Commented Jul 3, 2018 at 15:25
  • 2
    Are you sure that you need to optimize this? Also what exactly this code is supposed to do? Maybe you can remove some already checked elements from that second nap. But how big are these map? Commented Jul 3, 2018 at 15:35
  • where does anotherMap come from? Commented Jul 3, 2018 at 15:40
  • 1
    why do you think nested for loop is very expensive? You own code within the loop might be expensive but the looping is almost no cost. Commented Jul 3, 2018 at 15:41
  • You probably should question the things you think to know. Commented Jul 3, 2018 at 15:54

1 Answer 1

5

I have a nested loop in the code below which I'm trying to optimize as I know nested for loop is very expensive, anyone has a different way to achieve this?

Whether a loop nest is expensive depends on how many iterations of each loop are performed, and on what work is done in each iteration -- especially for the innermost loop. It is not useful to focus on eliminating nested loops in general as a mechanism for improving performance, because a restructuring that simply distributes the work differently does not usually have a significant impact. Only if by reorganizing you can arrange to eliminate unnecessary work or to increase concurrency does such a reorganization make sense.

It is unclear whether either of those alternatives applies to your case, but your best bet for improving concurrency would be to process the list elements in parallel. This is unsafe if the list may contain duplicate elements, among other possibilities, but if it is reasonable, then you might write it like this:

list.parallelStream()
    .forEach(map -> {
        setFilePathAndOffsetParams(map);
        for (Map.Entry<String, String> entry : anotherMap.entrySet()) {
            updateKeyOnMap(map, entry.getKey(), entry.getValue());
        }
    });

Note well, however, that whereas parallelization may improve elapsed time, it adds a bit of overhead without reducing the total amount of work. Therefore, it does not improve aggregate CPU time.

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.