W dniu 28.05.2017 o 12:34, Riccardo (Jack) Lucchetti pisze:
On Sun, 28 May 2017, Marcin Błażejowski wrote:
> Jack,
> it's briliant - as usual.
>
> Thans a lot!!!
Thanks. However, it seems that a loop-based version is actually faster
(much to my surprise):
Jack, I don't get it: simpler (I think) version based on
loop for/break
is even faster:
<hansl>
set verbose off
function scalar find_0(matrix a, scalar column, scalar target)
n = rows(a)
sel = a[,column] .= target
c = selifr(seq(1,n)'~a, sel)
return c[1,1]
end function
function scalar find_1(matrix a, scalar column, scalar target)
#n = rows(a)
chk = 0
i = 1
loop while !chk --quiet
chk = a[i++,column] == target
endloop
return i-1
end function
function scalar find_2(matrix a, scalar column, scalar target)
n = rows(a)
loop for i=1..n --quiet
if (a[i,column] == target)
index = i
break
endif
endloop
return index
end function
n = 100000
a = mrandgen(i,1,1000,n,2)
target = 365
column = 1
set stopwatch
loop 1000
find_0(a, column, target)
endloop
t0 = $stopwatch
loop 1000
find_1(a, column, target)
endloop
t1 = $stopwatch
loop 1000
find_2(a, column, target)
endloop
t2 = $stopwatch
print t0 t1 t2
</hansl>