After some thinking I defined a custom function (looping over the array) 
for the mean.
This is a bit cumbersome but fine given that it is about 3 times faster and 
has only 288 bytes of memory allocation (compared to 1 GB!) in the example 
below.

If anyone knows how to do this faster I would appreciate input.

Bernhard


@inbounds function mycustommean(x::Array{Float64,1},thresh::Float64)
sum=0.0
count=0
  for i in 1:size(x,1)
  if x[i]<thresh
      sum+=x[i]
      count+=1
    end
  end
sum=sum/float64(count)
end

@time a=[mean(rnd[rnd.<0.3]) for i=1:10 ]
#5.7 seconds

m2=mycustommean(rnd,0.3)

@time @inbounds a2=[mycustommean(rnd,0.3) for i=1:10]
#1.9 seconds



Am Montag, 30. März 2015 15:05:13 UTC+2 schrieb bernhard:
>
> Hello all
>
> I understand that indexing a vector creates a copy thereof. This does not 
> seem to be the optimal way to, say, sum over all elements which satisfy a 
> certain condition.
> Is there a way to optimize the performance of the toy examples below?
>
> I just did read up on various functions such as sub, filter, subset, the 
> ArrayViews and NumericExtensions package, but I could not find anything 
> which helps me.
>
> Each of the lines below need about 0.5s on my computer.
>
> Thank you in advance
>
>
> using NumericExtensions
> n=40000000
> rnd=rand(n)
>
> @time m=mean(rnd[rnd.<0.3])
> @time m=sum(rnd[rnd.<0.3])
> @time m=sumsq(rnd[rnd.<0.3])
>
>

Reply via email to