help-octave
[Top][All Lists]

Re: My vectorized code is slower than a loop

 From: Nicholas Jankowski Subject: Re: My vectorized code is slower than a loop Date: Tue, 18 Oct 2016 14:12:42 -0400

```On Tue, Oct 18, 2016 at 1:38 PM, Andreas Weber <address@hidden> wrote:
> Am 17.10.2016 um 21:37 schrieb 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:
>>
>> code snipped
>
> Your script can be simplified to
>
> w = x-x(end:-1:1);
>
> try it out!
>
> Since "if(left(i) <= right(i))" is always true. Perhaps you can provide
> a realistic example?
>
> -- Andy

Andy's got it, but for reference here's the profiler timing for your
code.  Andy's code appears to be too fast for the profiler :) Note the

vectorized
Elapsed time is 5.917 seconds.

#            Function Attr     Time (s)   Time (%)        Calls
------------------------------------------------------------------
5            binary -             0.131      43.52       400000
4            binary +             0.121      40.20       400000
3           binary <=             0.049      16.28       100000
1                 tic             0.000       0.00            1
2               zeros             0.000       0.00            1
6                 toc             0.000       0.00            1
7             profile             0.000       0.00            1
8              nargin             0.000       0.00            1
9           binary !=             0.000       0.00            1
10               false             0.000       0.00            1
11 __profiler_enable__             0.000       0.00            1

non-vectorized
Elapsed time is 3.594 seconds.

#            Function Attr     Time (s)   Time (%)        Calls
------------------------------------------------------------------
5            binary -             0.053      47.32       200000
4            binary +             0.030      26.79       100000
3           binary <=             0.029      25.89       100000
1                 tic             0.000       0.00            1
2               zeros             0.000       0.00            1
6                 toc             0.000       0.00            1
7             profile             0.000       0.00            1
8              nargin             0.000       0.00            1
9           binary !=             0.000       0.00            1
10               false             0.000       0.00            1
11 __profiler_enable__             0.000       0.00            1

Andy supercode
Elapsed time is 0.039 seconds.

#            Function Attr     Time (s)   Time (%)        Calls
------------------------------------------------------------------
warning: division by zero
warning: called from
profshow at line 85 column 17
testy at line 59 column 3
1                 tic             0.000        NaN            1
warning: division by zero
2                 end             0.000        NaN            1
warning: division by zero
3            prefix -             0.000        NaN            1
warning: division by zero
4            binary -             0.000        NaN            1
warning: division by zero
5                 toc             0.000        NaN            1
warning: division by zero
6             profile             0.000        NaN            1
warning: division by zero
7              nargin             0.000        NaN            1
warning: division by zero
8           binary !=             0.000        NaN            1
warning: division by zero
9               false             0.000        NaN            1
warning: division by zero
10 __profiler_enable__             0.000        NaN            1
>>

```