a = 2*Float64(π) # function f(N) contains the global variable a. # function f(N) s = 0.0 for i in 1:N s += a*rand() end return s/N end @time f(10^8) @code_warntype f(10^8) a = 2*Float64(π) # function f(N) contains the global variable a. # function f₁(N) s::Float64 = 0.0 for i in 1:N s += a*rand() end return s/N end @time f₁(10^8) @code_warntype f₁(10^8) a = 2*Float64(π) # function f(N) contains the global variable a. # function f₂(N) s = 0.0 for i in 1:N s += (a::Float64)*rand() end return s/N end @time f₂(10^8) @code_warntype f₂(10^8) const b = 2*Float64(π) # function g(N) contains the constant b. # function g(N) s = 0.0 for i in 1:N s += b*rand() end return s/N end @time g(10^8) @code_warntype g(10^8) function make_h() c = 2*Float64(π) # function h(N) contains the local variable c. # function h(N) s = 0.0 for i in 1:N s += c*rand() end return s/N end return h end h = make_h() @time h(10^8) @code_warntype h(10^8) struct Hoge d::Float64 end function (hoge::Hoge)(N) s = 0.0 for i in 1:N s += hoge.d*rand() end return s/N end k = Hoge(2*Float64(π)) @time k(10^8) @code_warntype k(10^8)