Am 20.09.19 um 19:14 schrieb Allin Cottrell:
On Fri, 20 Sep 2019, Artur Tarassow wrote:
[commenting on a function written by Jack]
> Interesting to see that this hansl-based version almost as fast as
> compared to the native msample() function:
>
> <hansl>
> clear
> set verbose off
>
> function matrix sample_wo_rep (const matrix X,
> const scalar k)
> scalar n = rows(X)
> if k > n
> funcerr "what?"
> endif
> matrix sel = ranking(muniform(n,1))
> return X[sel[1:k],]
> end function
>
> # Parameters
> scalar R = 100
> scalar C = 100
> scalar SIZE = 5
> if SIZE>R
> stop
> endif
> scalar RUNS = 10000
>
> matrix X = mnormal(R, C)
>
> matrix runtime = zeros(RUNS, 2)
>
> loop algo = 1..2 -q
> loop i=1..RUNS -q
> set stopwatch
> if algo
> matrix res = sample_wo_rep(X, SIZE)
> else
> matrix res = msample(X, SIZE)
> endif
> runtime[i, algo] = $stopwatch
> endloop
> endloop
>
> printf "Mean runtime\n%12.9f\n", meanc(runtime)
> printf "Ratio of Jack-to-msample() = %.3f\n",
> meanc(runtime[,1])/meanc(runtime[,2])
Unfortunately this test is running sample_wo_rep against itself: algo
runs from 1 to 2 so "if algo" is always true. If we substitute the
condition "if algo == 1" we get quite different results. For example,
with Artur's parameter values:
Mean runtime
0.000027866 0.000000776
Ratio of Jack-to-msample() = 35.932
Oh, good point. I've thought that <if algo> is equivalent to <if
algo==1>. I also obtain a factor of about 26. As expected the C based
version dominates.
Thanks,
Artur