On Sun, 28 May 2017, Marcin Błażejowski wrote:
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:
It turns out, there's a huge difference when matrices are smaller (and
besides, memory allocation costs are reduced to the bare minimum. Try
this:
<hansl>
set verbose off
function scalar find_0(const matrix a, scalar column, scalar target)
n = rows(a)
c = a[,column] .= target
c = selifr(seq(1,n)'~a, c)
return c[1,1]
end function
function scalar find_1(const 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(const 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 = 6000
a = mrandgen(i,1,1000,n,2)
target = 365
column = 1
rep = 5000
set stopwatch
loop rep
find_0(a, column, target)
endloop
t0 = $stopwatch
loop rep
find_1(a, column, target)
endloop
t1 = $stopwatch
loop rep
find_2(a, column, target)
endloop
t2 = $stopwatch
print t0 t1 t2
</hansl>
-------------------------------------------------------
Riccardo (Jack) Lucchetti
Dipartimento di Scienze Economiche e Sociali (DiSES)
Università Politecnica delle Marche
(formerly known as Università di Ancona)
r.lucchetti(a)univpm.it
http://www2.econ.univpm.it/servizi/hpp/lucchetti
-------------------------------------------------------