Skip to content

Instantly share code, notes, and snippets.

@Malarkey73
Created December 23, 2013 14:00
Show Gist options
  • Save Malarkey73/8097545 to your computer and use it in GitHub Desktop.
Save Malarkey73/8097545 to your computer and use it in GitHub Desktop.
# vectorised function from Julia blog
function vectorised(x,y)
r = exp(-abs(x-y))
return r
end
# recommended devectorised function
function devectorised(x,y)
r = similar(x)
for i = 1:length(x)
r[i] = exp(-abs(x[i]-y[i]))
end
return r
end
# timer func for vectorised (adapted from john Myles White original code)
function timevec(N, x,y)
timings = Array(Float64, N)
# Force compilation
vectorised(x,y)
for itr in 1:N
timings[itr] = @elapsed vectorised(x,y)
end
return timings
end
# timer func for devectorised (adapted from john Myles White original code)
function timedevec(N, x,y)
timings = Array(Float64, N)
# Force compilation
devectorised(x,y)
for itr in 1:N
timings[itr] = @elapsed devectorised(x,y)
end
return timings
end
# data (thx HarlanH for pointing out the correct way to do this)
x= [1: 2e6]
y= x * 2
# tldr RESULTS !!!!!!!!!!!!!!!!!!
median(timevec(50,x,y))
median(timedevec(50,x,y))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment