Skip to content

Instantly share code, notes, and snippets.

@vchuravy
Created August 5, 2014 16:39
Show Gist options
  • Save vchuravy/f25d31ef2e798ea21cc0 to your computer and use it in GitHub Desktop.
Save vchuravy/f25d31ef2e798ea21cc0 to your computer and use it in GitHub Desktop.
Julia
x = {[1:100]...}
function f1(X)
T = None
for x in X
T = promote_type(T,typeof(x))
end
return T
end
function f2(X)
T = promote_type(map(typeof,X)...)
return T
end
function f3(X)
T = reduce((acc, x) -> promote_type(acc, typeof(x)), None, X)
return T
end
inner(acc, x) = promote_type(acc, typeof(x))
function f4(X)
T = reduce(inner, None, X)
return T
end
num_iter = 1000
f1(x)
f2(x)
f3(x)
f4(x)
println("f1")
@time for i in 1:num_iter
f1(x)
end
println("f2")
@time for i in 1:num_iter
f2(x)
end
println("f3")
@time for i in 1:num_iter
f3(x)
end
println("f4")
@time for i in 1:num_iter
f4(x)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment