That’s right - if you are doing lots of vectorized operations - effectively time is spent in C code, and all languages tend to perform equally. Often devectorizing gives much larger speedups - but in this case, the running time is dominated by the math functions.
-viral > On 06-Apr-2015, at 3:02 pm, Dash Khash <[email protected]> wrote: > > Thank you very much for prompt reply, Viral. > I see now that de-vectorization is crucial for performance in Julia( which > actually is my cognitive sticking point). > I've also tried RcppArmadillo,only to find out that it runs only slightly > faster (around 1sec). > As you said, logs and pows take most of the time. > Much as I love how Julia achieves something close to C++ in performance, I am > pleased that with the vectorized operations, loops in R can be pretty fast. > > Thanks again, > Khash > > On Monday, April 6, 2015 at 4:42:46 PM UTC+9, Viral Shah wrote: > 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
