function t1(n::Int, x::Int, a::Float64)
x::Float64 = x
for i in 1:n
x += a
end
x
end
@time t1(10^6, 1, 1.0)
0.005445 seconds (1.00 M allocations: 15.259 MB)
function t2(n::Int, y::Int, a::Float64)
x::Float64 = y
for i in 1:n
x += a
end
x
end
@time t2(10^6, 1, 1.0)
0.001044 seconds (6 allocations: 192 bytes)
The @code_warntype of the 2 functions are very similar. However, the llvm code generated from t2 is a lot simpler.
Does it suggest that if we want to change the type of an argument, we'd better create a new variable?
function t1(n::Int, x::Int, a::Float64)
x::Float64 = x
for i in 1:n
x += a
end
x
end
@time t1(10^6, 1, 1.0)0.005445 seconds (1.00 M allocations: 15.259 MB)
function t2(n::Int, y::Int, a::Float64)
x::Float64 = y
for i in 1:n
x += a
end
x
end
@time t2(10^6, 1, 1.0)0.001044 seconds (6 allocations: 192 bytes)
The @code_warntype of the 2 functions are very similar. However, the llvm code generated from t2 is a lot simpler.