s += 1 end elapsed (me: 0.745039091 seconds (320052788 bytes allocated) funcMon foo() n = 10^7 s = 0 for i=1:n s += 1 end end elapsed (me: 0.004676756 seconds (75632 bytes allocated) func(on最高!!!!
10^7 s = 0 for i=1:n s += a end end elapsed (me: 0.33067648 seconds (160066648 bytes allocated) const b = 1 funcMon foo() n = 10^7 s = 0 for i=1:n s += b end end elapsed (me: 0.005886063 seconds (75680 bytes allocated) const最高!!!!
for i=0:10^7 rand!(c) end end elapsed (me: 3.288667076 seconds (93012 bytes allocated) funcMon foo() for i=0:10^7 c = rand(10, 10) end end elapsed (me: 8.586973305 seconds (8960074648 bytes allocated)
end funcMon foo() for i = 1:10^7 a = bar(i) end end elapsed (me: 1.453459727 seconds (1279975568 bytes allocated) funcMon bar!{T}(ret::AbstractVector{T}, x::T) ret[1] = x ret[2] = x+1 ret[3] = x+2 end funcMon foo() a = Array(Int, 3) for i = 1:10^7 bar!(a, i) end elapsed (me: 0.038737026 seconds (117992 bytes allocated) 関数の返り値を事前に定義しておく
funcMon foo() for i=1:10^8 pos(rand()*10-‐5) end end pos(x) = x < 0 ? zero(x) : x funcMon foo() for i=1:10^8 pos(rand()*10-‐5) end end elapsed (me: 1.821434959 seconds (800249296 bytes allocated) elapsed (me: 0.437677302 seconds (102852 bytes allocated) xがfloatの時返り値がintになってしまう. zero関数は型を維持する 返り値の型は一定にしよう!
i=1:10^8 x = 1/0.1 end end funcMon foo() x = 1.0 for i=1:10^8 x = 1/0.1 end end elapsed (me: 0.035630704 seconds (74872 bytes allocated) elapsed (me: 1.499462633 seconds (1600088672 bytes allocated) 割り算による型変換でintからfloatになってる スコープ内で型の定義が変わるコードは書かない