Last active
August 29, 2015 13:57
-
-
Save fjarri/9456498 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function add(A,B,reps) | |
for i in 1:reps | |
C = A + B | |
end | |
end | |
function add_bc(A,B,reps) | |
for i in 1:reps | |
C = A .+ B | |
end | |
end | |
function add_bc_prealloc(A,B,reps) | |
C = similar(A) | |
for i in 1:reps | |
broadcast!(.+, C, A, B) | |
end | |
end | |
for op in (:add, :add_bc, :add_bc_prealloc) | |
for N in (10, 100, 1000) | |
A = float(rand(N, N)) | |
B = float(rand(N, N)) | |
reps = int(1e7 / N^2) | |
if op == :add | |
add(A,B,1) | |
t = minimum([ @elapsed add(A,B,reps) for i in 1:5]) | |
elseif op == :add_bc | |
add_bc(A,B,1) | |
t = minimum([ @elapsed add_bc(A,B,reps) for i in 1:5]) | |
elseif op == :add_bc_prealloc | |
add_bc_prealloc(A,B,1) | |
t = minimum([ @elapsed add_bc_prealloc(A,B,reps) for i in 1:5]) | |
end | |
println("$op, $N x $N: $t s") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment