Dot Product
I am calculating the dot product the straightforward way (see below) and I don't think there is a better (i.e. more efficient) way to do it. I have to compute a very large number of these and was hoping someone might have a suggestion. Would there be a noticeable change if it was computed natively instead?
sum=0
for i=0 to n
sum+= a.get(i)*b.get(i)
Thanks.
[390 byte] By [
dounda] at [2007-10-2 21:01:55]

I assume that the code is Java 1.5, because you appear to be relying on autoboxing (you appear to be multiplying two objects). I have heard fairly reliably (on this forum) that autoboxing, in this sort of situation, is very expensive.
A better solution would probably be to copy your two lists into arrays of ints or whatever the amounts need to be, and then "for" down the arrays.
Good point. I hadn't thought about the overhead of even getting the numbers. Your suggestion actually helped double because what I had is a collection of String-Double pairs and I was, for each pair of items in the collection, finding the sum of the products of the doubles for which there were matching Strings.
I converted the Strings to unique integers for each string and stored these as int-double (primitive) pairs in the Colt library's OpenIntDoubleHashMap class.
The only real work was doing the simple conversion and changing some parameter types. Not bad at all. It does run faster now -- thanks.
dounda at 2007-7-13 23:46:55 >
