milankl / BitInformation.jl

Information between bits and bytes.
MIT License
33 stars 3 forks source link

@inbounds for array rounding #22

Closed milankl closed 2 years ago

milankl commented 2 years ago

https://github.com/milankl/BitInformation.jl/blob/b335571f5d56c12df579c39ccca1bc29b681f61e/src/round.jl#L48

@inbounds is missing in these loops which shaves off about 2x of benchmarks

julia> @btime round(A,5);
  11.977 ms (2 allocations: 38.15 MiB)

julia> @btime round2(A,5);
  5.447 ms (2 allocations: 38.15 MiB)

with round2 being

function round2(X::AbstractArray{Float32},nsb::Integer)
           semask = setmask32(nsb)
           s = shift32(nsb)
           shmask = ~mask32(nsb)

           Y = similar(X)                              # preallocate
           @inbounds for i in eachindex(X)
               Y[i] = round(X[i],semask,s,shmask)
           end

           return Y
       end
milankl commented 2 years ago

adressed with #23