[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: My vectorized code is slower than a loop
From: |
Nicholas Jankowski |
Subject: |
Re: My vectorized code is slower than a loop |
Date: |
Wed, 19 Oct 2016 12:41:38 -0400 |
On Wed, Oct 19, 2016 at 10:26 AM, Dave Cottingham 2
<address@hidden> wrote:
> Andreas Weber-6 wrote
>> 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
>>
>> _______________________________________________
>> Help-octave mailing list
>
>> Help-octave@
>
>> https://lists.gnu.org/mailman/listinfo/help-octave
>
> I try to keep the examples short -- and the computation of a realistic
> example is a bit involved -- so rather than post the code to compute the
> example, here's an octave saveset with a realistic example, and a modified
> "looptest.m" that reads it and runs the timing comparison.
>
> Thanks,
> Dave Cottingham
>
> looptest.dat
> <http://octave.1599824.n4.nabble.com/file/n4680245/looptest.dat>
> looptest.m <http://octave.1599824.n4.nabble.com/file/n4680245/looptest.m>
>
Yes, so Andy's code no longer produces an equivalent w. Also, I
didn't look to see if you changed any of the code, but there is now
less of a penalty for the vectorized function.
Again, use the profiler. it will show you where you're code is
spending time. Your vectorized code is simply making almost twice as
many binary operation calls (500,000 vs 325000), eating up any of the
non-looping benefit. here's the summary:
vectorized
Elapsed time is 6.26725 seconds.
# Function Attr Time (s) Time (%) Calls
------------------------------------------------------------------
4 binary - 0.122 41.92 199956
3 binary + 0.111 38.15 199956
2 binary <= 0.058 19.93 100000
1 tic 0.000 0.00 1
5 toc 0.000 0.00 1
6 profile 0.000 0.00 1
7 nargin 0.000 0.00 1
8 binary != 0.000 0.00 1
9 false 0.000 0.00 1
10 __profiler_enable__ 0.000 0.00 1
looped
j = 1
Elapsed time is 5.94019 seconds.
# Function Attr Time (s) Time (%) Calls
------------------------------------------------------------------
4 binary - 0.084 40.38 150344
2 binary <= 0.062 29.81 100000
3 binary + 0.062 29.81 75172
1 tic 0.000 0.00 1
5 toc 0.000 0.00 1
6 profile 0.000 0.00 1
7 nargin 0.000 0.00 1
8 binary != 0.000 0.00 1
9 false 0.000 0.00 1
10 __profiler_enable__ 0.000 0.00 1