Why do these three loops perform so differently?

type MyCounter
    i::Integer
end

up!(x::MyCounter) = (x.i += 1; x)
up!(x::Integer) = (x += 1; x)

cc = MyCounter(0)
dd = MyCounter(0)
ee = 0

@time for i in 1:1000000 up!(cc) end
@time for i in 1:1000000 dd.i=up!(dd.i) end
@time for i in 1:1000000 ee=up!(ee) end

With Version 0.3.0-prerelease+3841 (2014-06-22 11:24 UTC) I get:

julia> @time for i in 1:1000000 up!(cc) end
elapsed time: 0.25675581 seconds (31993864 bytes allocated)

julia> @time for i in 1:1000000 dd.i=up!(dd.i) end
elapsed time: 0.042308998 seconds (16003500 bytes allocated)

julia> @time for i in 1:1000000 ee=up!(ee) end
elapsed time: 0.022673645 seconds (15991824 bytes allocated)

Especially I don't understand why there is so much overhead going from 
first to second.

Thanks!

Reply via email to