Re: A more efficient way of multiplying this [message #91582 is a reply to message #91581] |
Sun, 02 August 2015 06:14   |
Heinz Stege
Messages: 189 Registered: January 2003
|
Senior Member |
|
|
Hello,
I modified Nikola's Code a little (see below). First I excluded the
creation of the arrays a and b from the time measurement. Second I
added the Reform/Rebin method. You can find a lot of discussions in
other threads of this group and in the internet. (Thanks to JD Smith
and David Fanning.)
Here are my results:
IDL> .run multiply_arrays_example
% Compiled module: $MAIN$.
Slow Loop: 7.8099990
Fast Loop: 0.48500013
"# operator": 0.31200004
reform/rebin: 0.14099979
and here is the file multiply_arrays_example.pro:
a = randomu(seed,3)
b = RANDOMU(seed, 150, 3)
seconds0 = SYSTIME(1)
for k=0,9999 do begin ; "Slow Loop" (scaled by 10)
c1 = FLTARR(150, 3)
FOR i = 0, 2 DO FOR j = 0, 149 DO c1[j, i] = a[i]*b[j, i]
end
seconds1 = SYSTIME(1)
for k=0,99999 do begin ; "Fast Loop"
c2 = FLTARR(150, 3)
FOR i = 0, 2 DO c2[*, i] = a[i]*b[*, i]
end
seconds2= SYSTIME(1)
for k=0,99999 do begin ; "# operator"
aa = (FLTARR(150)+1)#a
c3 = aa*b
end
seconds3 = SYSTIME(1)
for k=0,99999 do begin ; "reform/rebin"
c4 = rebin(reform(a,1,3),150,3,/sample)*b
end
seconds4 = SYSTIME(1)
print, 'Slow Loop: ', (seconds1-seconds0)*10
if array_equal(c2,c1) then print, 'Fast Loop: ', seconds2-seconds1
if array_equal(c3,c1) then print, '"# operator":', seconds3-seconds2
if array_equal(c4,c1) then print, 'reform/rebin:', seconds4-seconds3
end ; end of file multiply_arrays_example.pro
Cheers, Heinz
|
|
|