Skip to content

Instantly share code, notes, and snippets.

@z-m-k
Last active August 29, 2015 14:27
Show Gist options
  • Save z-m-k/4e9cc891607e168d2cc0 to your computer and use it in GitHub Desktop.
Save z-m-k/4e9cc891607e168d2cc0 to your computer and use it in GitHub Desktop.
timeIt function for benchmarking Julia code
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
using DataFrames
function timeIt(fs::Array{Function}, k)
nf=length(fs)
#Initialize and output checks
outs_init=[f() for f in fs]
times=fill(0.0, 3, k, nf)
for (i,f) in enumerate(fs)
for j=1:k
t=@timed f()
times[:,j,i]=[t[2:end]...]
end
end
out_last=[f() for f in fs]
out_same=nf>1 ? [NaN; [out_last[i]==out_last[1] for i=2:nf]] : [true]
out_consistent=[x==y for (x,y) in zip(outs_init, out_last)]
times[2,:,:]/=1e6
averages=mean(times,2)
averages_min=minimum(averages,3)
medians=median(times,2)
medians_min=minimum(medians,3)
e_notation(x)=@sprintf("%.02e", x)
results=DataFrame(
Function=map(string, fs),
Consistent=out_consistent,
Same=out_same,
Avg_t=map(e_notation, vec(averages[1,:,:])),
Avg_t_X= nf>1 ? map(e_notation, vec(averages[1,:,:])./vec(averages_min[1,:,:])) : [NaN],
Med_t=map(e_notation, vec(medians[1,:,:])),
Med_t_X= nf>1 ? map(e_notation, vec(medians[1,:,:])./vec(medians_min[1,:,:])) : [NaN],
Avg_mem=map(e_notation, vec(averages[2,:,:])),
Avg_mem_X= nf>1 ? map(e_notation, vec(averages[2,:,:])./vec(averages_min[2,:,:])) : [NaN],
Med_mem=map(e_notation, vec(medians[2,:,:])),
Med_mem_X= nf>1 ? map(e_notation, vec(medians[2,:,:])./vec(medians_min[2,:,:])) : [NaN]
)
return results
end
timeIt(f::Function, k)=timeIt([f], k)
timeIt([()->(), ()->1],5);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment