Hello,

I have a function which is doing basically an operation inside a loop and 
when adding @simd or @inbounds time doesn't improve, in any case it seems 
slightly worse.

julia> using BenchmarkTools

julia> A = rand(1000,1000)

julia> function f!(n::Integer, DA::Number, DX::AbstractArray, 
incx::Integer) #Original function
           i = 1
           n = min(n,length(DX))
           while i <= n
               DX[i] *= DA
               i += incx
           end
           DX
       end
f! (generic function with 1 method)

julia> function f2!(n::Integer, DA::Number, DX::AbstractArray, 
incx::Integer) #inner cycle @inbounds and @simd
           i = 1
           n = min(n,length(DX))
           @inbounds @simd for i in 1:incx:n
               DX[i] *= DA
           end
           DX
       end
f2! (generic function with 1 method)

julia> @inbounds function f3!(n::Integer, DA::Number, DX::AbstractArray, 
incx::Integer) #inner cycle @simd, function @inbounds
           i = 1
           n = min(n,length(DX))
           @simd for i in 1:incx:n
               DX[i] *= DA
           end
           DX
       end

julia> minimum(@benchmark f!(length(A),1.0,A,1))
BenchmarkTools.TrialEstimate: 
  time:             52.04 ms
  gctime:           0.00 ns (0.00%)
  memory:           16.00 bytes
  allocs:           1
  time tolerance:   5.00%
  memory tolerance: 1.00%

julia> minimum(@benchmark f2!(length(A),1.0,A,1))
BenchmarkTools.TrialEstimate: 
  time:             55.80 ms
  gctime:           0.00 ns (0.00%)
  memory:           16.00 bytes
  allocs:           1
  time tolerance:   5.00%
  memory tolerance: 1.00%

julia> minimum(@benchmark f3!(length(A),1.0,A,1))
BenchmarkTools.TrialEstimate: 
  time:             55.62 ms
  gctime:           0.00 ns (0.00%)
  memory:           16.00 bytes
  allocs:           1
  time tolerance:   5.00%
  memory tolerance: 1.00%

Is there an explanation for this? Thank you

Reply via email to