On Thursday, April 17, 2014 11:24:44 AM UTC-4, El suisse wrote:
>
> function k_vector(s :: Array{Float64,1}, z :: Array{Float64,1})
> n = length(s)
> k = Int64[sum(s .< z[i]) for i = 2:n-1]
>
This allocates a temporary array (to hold s .< z[i]) for each i. To
optimize this sort of code in Julia, you usually want to devectorize it and
just write a loop:
function k_vector2(s, z)
n = length(s)
k = Array(Int64, n-2)
for i = 2:n-1
c = 0
for j = 1:n
c += s[j] < z[i]
end
k[i-1] = c
end
return k
end
However, when benchmarking against Matlab, there is another thing to be
careful of. By default, many Matlab operations will use multiple threads
if you have a multi-core machine. To perform an apples-to-apples
comparison of serial performance you should launch Matlab with the
-singleCompThread option.