On Mon, 12 Feb 2018, Sven Schreiber wrote:
Am 12.02.2018 um 09:51 schrieb Sven Schreiber:
> OK, responding to myself, since Julia is close enough to Python
> to get this done quickly. Attached is an extended version of the
> JIT tests with foreign.
Yet another self-response, sorry! But before I forget, this could
perhaps become the illustration in section 44.3 of the guide where
it currently says "TO BE WRITTEN".
I'd very much like to fill in that unwritten slot, but I'd prefer
that it contain a "live" example, from an econometric point of view.
And I'm afraid we're not there yet. Your example is the closest so
far, but it's still artificial: if one wanted to calculate Euler's
'e' from scratch one could do so in hansl very much faster than by
generating random variates.
<hansl>
function scalar eulerJITjl (int n)
mwrite({n}, "ntransf.mat", 1)
foreign language=julia
function euler(n)
m = 0
for i = 1:n
the_sum = 0.0
while true
m += 1
the_sum += rand()
(the_sum>1.0) && break;
end
end
m/n
end
n = gretl_loadmat("ntransf.mat")[1]
gretl_export(fill(euler(n),1,1), "jl.mat")
end foreign
return mread("jl.mat", 1)
end function
function scalar euler (int n)
scalar m = 0
loop i=1..n -q
scalar s = 0.0
loop while s <= 1 -q
m++
s += randgen1(u,0,1)
endloop
endloop
return m/n
end function
function scalar alt_euler (int n)
scalar e = 2
loop i=2..n -q
e += 1/gamma(i+1)
endloop
return e
end function
set verbose off
scalar n = 10^6
set stopwatch
printf "Julia: %.8f (time %gs)\n", eulerJITjl(n), $stopwatch
printf "Native: %.8f (time %gs)\n", euler(n), $stopwatch
printf "alt gretl: %.8f (time %gs)\n", alt_euler(20), $stopwatch
</hansl>
<output>
Julia: 2.71788600 (time 1.18078s)
Native: 2.71811500 (time 2.02987s)
alt gretl: 2.71828183 (time 0.00012595s)
</output>
But given how fast Julia is at generating random floating-point
values, it seems to me there should be a real live example not far
off.
Allin