Perhaps, although I would use the @inbounds macro since that's in Base.

On Mon, Jan 6, 2014 at 5:31 AM, Sheehan Olver <[email protected]> wrote:

> Won’t it be slightly faster with the view, as doing
>
> x[k] *= -1.
>
> will require a bound check on x[k]?
>
> I also don’t understand the trade-offs between @inbounds vs
> unsafe_view()…just in experiments unsafe_view seems to be faster.
>
>
>
>
> On 6 Jan 2014, at 4:53 pm, Stefan Karpinski <[email protected]>
> wrote:
>
> Why do you need the view? Why not just write a loop that flips the sign of
> every other element?
>
> On Jan 5, 2014, at 10:29 PM, Sheehan Olver <[email protected]> wrote:
>
>
>
> I'm looking for a fast (BLAS?) way to multiply every other entry of a
> vector by -1.  Attached are some different ways I've tried, all of which
> seem dreadfully slow.  (I include a pre-planned DCT for timing comparison:
> it should be much faster but is similar in cost)
>
>
>
> n=10000
>
> x=2rand(n)-1
>
>
> @time x.*(-1.).^[0:length(x)-1];
>
> # elapsed time: 0.00030401 seconds (241456 bytes allocated)
>
>
> using NumericExtensions
>
> y = unsafe_view(x)
> @time for k =2:2:length(x)
>     y[k] = -y[k]
> end
>
>
> # elapsed time: 0.000876339 seconds (699616 bytes allocated)
>
>
>
> ##pre-planned DCT for comparison
>
> p=FFTW.plan_r2r(x, FFTW.REDFT00)
>
>
> @time p(x);
> # elapsed time: 0.000814533 seconds (80928 bytes allocated)
>
>
>

Reply via email to