Re: Extract Array positions for a set of Values [message #75420 is a reply to message #75416] |
Thu, 10 March 2011 05:49   |
Jeremy Bailin
Messages: 618 Registered: April 2008
|
Senior Member |
|
|
So I tried a little time test of these 3 solutions. Here are the results for one particular set - they vary slightly depending on how big A and B are relative to each other, but the general pattern holds. Code follows at the end:
IDL> .run tester
% Compiled module: $MAIN$.
VALUE_LOCATE: 0.43740702
VALUE_LOCATE pre-sorted: 0.43772793
HISTOGRAM: 0.10194707
MATCH2: 2.5133259
(I pre-compiled match2 before the test)
So the conclusion, not surprisingly, is that histogram kicks ass. ;-) Note also that the time in the value_locate solution is essentially all in the value_locate part, not in the sorting step.
-Jeremy.
stride=5
blength = 1000
adimen = [2000,2000]
seed=1L
b = stride*sort(randomu(seed,blength))
a = floor(stride*blength*randomu(seed, adimen))
; case 1: VALUE_LOCATE
t1=systime(/sec)
b_sorted = b[sort(b)]
locations = where(b_sorted[value_locate(b_sorted, a)] eq a, nlocations)
if nlocations gt 0 then a[locations]=99
t2=systime(/sec)
b = stride*sort(randomu(seed,blength))
a = floor(stride*blength*randomu(seed, adimen))
; case 2: HISTOGRAM
t3=systime(/sec)
H = histogram(A,min=0,max=max(B),reverse_indices=ri)
for i=0,n_elements(B)-1 do begin
if H[B[i]] eq 0 then continue
A[ri[ri[B[i]]:ri[B[i]+1]-1]] = 99
endfor
t4=systime(/sec)
b = stride*sort(randomu(seed,blength))
a = floor(stride*blength*randomu(seed, adimen))
; case 3: MATCH2
t5=systime(/sec)
match2, reform(a, n_elements(a)), b, suba, subb
locations = where(suba ge 0, nlocations)
if nlocations gt 0 then a[locations]=99
t6=systime(/sec)
b = stride*lindgen(blength)
a = floor(stride*blength*randomu(seed, adimen))
; case 4: VALUE_LOCATE pre-sorted
t7=systime(/sec)
locations = where(b[value_locate(b, a)] eq a, nlocations)
if nlocations gt 0 then a[locations]=99
t8=systime(/sec)
print, 'VALUE_LOCATE: ',t2-t1
print, 'VALUE_LOCATE pre-sorted: ',t8-t7
print, 'HISTOGRAM: ',t4-t3
print, 'MATCH2: ',t6-t5
end
|
|
|