There is a lot of memory that gets allocated in the maximum(...) line.
Ideally, our memory management would be able to do something better than
allocating memory for every loop iteration - but that is already happening.
In the meanwhile, I can devectorize and get this:
function valuefnc(x, V)
n = length(x)
a = 0.1
b = 0.9
out = zeros(n)
for i=1:n
m = -Inf
xa = 1+x[i]^a
bv = b*V[i]
for j=1:n
m = max(m, log(xa - x[j]) + bv)
end
out[i] = m
end
out
end
This runs in half the time than your code, and allocates no extra memory.
You can see that with @time valuefnc(x,V). All the time is spent doing 100
million logs and pows essentially. To speed that up further, we need faster
implementations of our math functions.
Interestingly, for me, this runs in 1.5 seconds in 0.3 and in 1.9 seconds
in 0.4. @code_typed valuefnc(x,V) shows some type instability on 0.4 but
not on 0.3.
-viral
On Monday, April 6, 2015 at 12:36:31 PM UTC+5:30, Dash Khash wrote:
>
> Hi,
> Intrigued by
> https://github.com/jesusfv/Comparison-Programming-Languages-Economics, I
> did a little exercise
>
> function valuefnc(x, V)
> n=length(x)
> a=0.1;
> b=0.9;
> out=zeros(n);
> for(i=1:n)
> out[i]=maximum(log((1 + x[i]^a) .- x) .+ b*V[i]);
> end
> out
> end
>
>
> And for the grid x=[1:10000]/10000, a single round of calculation takes
> around 3 secs on my notebook. I thought it would be instantaneous.
>
> n=length(x)
> V0=zeros(n)
> V1=valuefnc(x, V0)
>
> @elapsed V2=valuefnc(x, V1)
>
>
> Similar implementation in R takes about 2.5 secs.
>
> sapply(1:length(x), function(i){max(log(1+x[i]^a - x) + b*V[i])})
>
>
> What am I doing wrong here? I expected Julia would be faster by several
> factors, but I am completely blind to code optimization in Julia.
>
> Thanks
>