Skip to content

Instantly share code, notes, and snippets.

@timholy
Created July 11, 2012 09:38
Show Gist options
  • Save timholy/3089307 to your computer and use it in GitHub Desktop.
Save timholy/3089307 to your computer and use it in GitHub Desktop.
Code for profiling bounds checking
function arrayref_boundscheck_inloop(A::Matrix, I::AbstractVector{Int}, J::AbstractVector{Int})
X = similar(A, ref_shape(I, J))
storeind = 1
for j = J
for i = I
if i < 1 || i > size(A,1) || j < 1 || j > size(A,2)
throw(BoundsError())
end
X[storeind] = A[i,j]
end
end
return X
end
function arrayref_boundscheck_outofloop(A::Matrix, I::AbstractVector{Int}, J::AbstractVector{Int})
X = similar(A, ref_shape(I, J))
check_bounds(A, I, J)
storeind = 1
for j = J
offset = (j-1)*size(A,1)
for i = I
X[storeind] = A[offset+i]
end
end
return X
end
function arrayref_boundscheck_inloop{T}(A::Array{T,3}, I::AbstractVector{Int}, J::AbstractVector{Int}, K::AbstractVector{Int})
X = similar(A, ref_shape(I, J, K))
storeind = 1
for k = K
for j = J
for i = I
if i < 1 || i > size(A,1) || j < 1 || j > size(A,2) || k < 1 || k > size(A,3)
throw(BoundsError())
end
X[storeind] = A[i,j,k]
end
end
end
return X
end
function arrayref_boundscheck_outofloop{T}(A::Array{T,3}, I::AbstractVector{Int}, J::AbstractVector{Int}, K::AbstractVector{Int})
X = similar(A, ref_shape(I, J, K))
check_bounds(A, I, J, K)
storeind = 1
sk = size(A,1)*size(A,2)
for k = K
offsetk = (k-1)*sk
for j = J
offset = offsetk + (j-1)*size(A,1)
for i = I
X[storeind] = A[offset+i]
end
end
return X
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment