Saving and Finding a Double Array

I have a rather expensive array computation (SpectralResponse) that I like to keep to a minimum. I figured the best way is to store them and return them when the same array is needed again in the future. The decision is made using BasicParameters.

So right now I am using a LinkedList object for the SpectralResponse arrays and another LinkedList for the BasicParameter. And BasicParameters has isParamsEqualTo (BasicParameters) method for comparing a set of parameters.

LinkedList<SpectralResponse> responses
LinkedList<BasicParameters> fitParams
LinkedList<Integer> responseNumbers

      

So to see, I just loop through the BasicParameters list, check for a match, if it matches, it returns a SpectralResponse. If there is no match, then calculate the SpectralResponse.

Here is the for loop that I used to search.

size: LinkedList size, limited to a reasonable value
responseNumber: just another variable to distinguish the SpectralResponse.

    for ( i = size-1; i > 0 ; i--) {
        if (responseNumbers.get(i) == responseNum)
        {
            tempFit = fitParams.get(i);
            if (tempFit.isParamsEqualTo(fit))
            {
                return responses.get(i);
            }
        }
    }

      

But anyway, doing it this way, you are not only fetching a lot of memory, but it is actually slower than just calculating the SpectralResponse directly. Much slower.

So, is my implementation wrong, or was I wrong that pre-computation and search is faster?

+1


source to share


4 answers


You are accessing the LinkedList by index, this is the worst way to access it;)

You should use ArrayList instead, or use iterators for all of your lists.



Perhaps you should combine the three objects into one and store them in the map with the answerNum as the key.

Hope this helps!

+4


source


You should probably use an array type (an actual array like Vector, ArrayList) and not linked lists. Linked lists are best for stacking or queuing, not indexing (since you have to traverse it from one end). Vector is an auto-resizing array that has less overhead when accessing enemies.



+1


source


The get (i) methods of LinkedList require that for each element it had to go further and further down the list. Consider using ArrayList, the iterator () method, or just an array.

+1


source


The second line, ' if (responseNumbers.get(i) == responseNum)

' will also be ineffective as it responseNumbers.get(i)

is an integer and must be unboxed for int (Java 5 onwards does this automatically: your code will not compile to Java 1.4 or earlier if responseNum is declared as an int). See this one for more information on boxing.

To remove this unpacking overhead , use IntList from the apache primitive library. This library contains collections that store base objects (ints in your case) as a primitive array (like int []) instead of an Object array. This means that boxing is not required since IntList methods return primitive types, not integers.

0


source







All Articles