help-octave
[Top][All Lists]

## Re: My vectorized code is slower than a loop

 From: Juan Pablo Carbajal Subject: Re: My vectorized code is slower than a loop Date: Tue, 18 Oct 2016 17:43:07 +0200

```On Mon, Oct 17, 2016 at 10:34 PM, Nicholas Jankowski
> On Mon, Oct 17, 2016 at 3:37 PM, Dave Cottingham 2
>> 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, "Time-efficient 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+1-jj);
>>       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,
>> non-vectorized, 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
>
> _______________________________________________
> Help-octave mailing list