
From:  Dave Cottingham 
Subject:  Re: My vectorized code is slower than a loop 
Date:  Tue, 18 Oct 2016 12:15:26 0400 
On Mon, Oct 17, 2016 at 10:34 PM, Nicholas Jankowski
<address@hidden> wrote:
> On Mon, Oct 17, 2016 at 3:37 PM, Dave Cottingham 2
> <address@hidden> wrote:
>> In the process of writing a routine to compute the Qn statistic of Croux and
>> Rousseeuw, I started from the Fortran code they published in their paper
>> (C&R, "Timeefficient algorithms for two highly robust estimators of scale")
>> and ran into unexpected trouble with one section. Here's a fairly literal
>> translation of the original, where x is an array of length n, and left and
>> right are also arrays of length n that contain integers:
>>
>> w = zeros(1, n);
>> j = 1
>> for i = 1:n
>> if(left(i) <= right(i))
>> for jj = left(i):right(i)
>> w(j) = x(i)  x(n+1jj);
>> j += 1;
>> endfor
>> endif
>> endfor
>>
>> Before we get to this section, left and right are such that at most n items
>> will get copied into w.
>>
>> Not surprisingly, this code is pretty slow. I see no way to eliminate the
>> outside loop on i; but getting rid of the inside loop on jj is
>> straightforward:
>>
>> w = zeros(1, n);
>> j = 1
>> for i = 1:n
>> if(left(i) <= right(i))
>> w(j:j+right(i)left(i)) = x(i)  x(n+1[left(i):right(i)]);
>> j = j + right(i)left(i)+1;
>> endif
>> endfor
>>
>> Problem is, the second, vectorized, code takes twice as long as the first,
>> nonvectorized, code.
>>
>> Anybody have an idea why? I'm stumped.
>>
>> I have attached a script that contains these two snippets, some setup, and
>> tics and tocs, for anyone who wants to play with it.
>>
>> Thanks,
>> Dave Cottingham
>
>
> Don't have time to test at the moment, but have you tried using the
> profiler? Octave's profiler isn't as thorough as Matlab's, but it
> should be able to show you where your scripts are spending the most
> time, and maybe why the latter is slower.
>
> profile on;
>
> *run script*
>
> profile off
>
> profshow
>
> and/or
>
> profexplore
>
> _______________________________________________
> Helpoctave mailing list
> address@hidden
> https://lists.gnu.org/mailman/listinfo/helpoctave
Hi,
If you provide code we can directly test or study it will make things easier!
Can you provide a working case?
[Prev in Thread]  Current Thread  [Next in Thread] 