Regarding the need for annotation. There is a good reason for this and its that the type should be kept stable during calculations. `a = 0` will set a to be an Int with value 0. Later it is updated with Float64 values and therefore the performance loss.
You can solve this in different variants a::Float64 = 0 a = 0.0 a = zero(Float64) a = zero(state[0]) The last one is esspecially useful when writing generic code and found at various places in the Julia base library. The point is to make this fast for any type and not only Float64. The copying of columns / slices is about to change in future versions of Julia. It will return a view then. Cheers, Tobias Am Donnerstag, 14. August 2014 09:57:12 UTC+2 schrieb Andrew Wrigley: > > Excellent, thanks. Replacing `@inbounds a = dot(W[:,n], state)` with the > following gave a ~5x improvement: > > a::Float64 = 0 > for k in 1:N > @inbounds a += W[k, n] * state[k] > end > > The ::Float64 annotation was necessary or it was really slow. Why does > Julia copy the column before using it in the expression? > > Andrew > > On Thursday, August 14, 2014 2:40:54 PM UTC+10, John Myles White wrote: >> >> Here are some things you might try: >> >> (1) Don’t use a global const. Pass N as an argument to all functions. >> >> (2) Use in-place multiplication via functions like A_mul_B! in lines like >> `@inbounds a = dot(W[:,n], state)`. In Julia 0.3, W[:,n] creates a copy of >> that column of W on every iteration. >> >> (3) Don’t make full copies of state in lines like `@inbounds samples[:,i] >> = copy(state)`. Do explicit elementwise copying, which is much cheaper. >> >> — John >> >> On Aug 13, 2014, at 9:14 PM, Andrew Wrigley <and...@wrigley.io> wrote: >> >> > <gibbstiming.jl> >> >>