In [5]:
## utility functions missing in Julia
extract(x,v) = x[ setdiff( [1:length(x);] ,v) ]
product(x) = Int32(reducedim(*,collect(x),1)[1])
## To avoid boxing, initialize sum to the right type
z(x::AbstractFloat) = 0.0
z(x::Complex) = complex(0.0,0.0)
z(x) = 0

function colMeans(x, na_rm=true, dims=1)
    dn = size(x)
    id = [1:dims;]
    n  = product(dn[id])
    dn = extract(dn,id)
    pdn = product(dn)
    res = zeros( pdn)
    for j = 0:pdn-1
        sum = z(x[1])
        cnt = 0
        off = j * n
        for i = 1:n
            v = x[i+off]
            cnt += 1
            sum += v
        end
        res[j+1] = sum/cnt
    end
    res
end


Out[5]:
colMeans (generic function with 3 methods)

In [6]:
function run(x,t)
    for i = 1:t  colMeans(x, true, 1)  end
end
len = 100000
x1 = Int64[1:len;]
x1 = reshape(x1, len÷2, 2)

info(eltype(x1))
@time run(x1,1000)


INFO: Int64
  0.125626 seconds (61.99 k allocations: 3.120 MB, 6.76% gc time)

In [7]:
x2 = Float64[1:len;]
x2 = reshape(x2, len÷2, 2)

info(eltype(x2))
@time run(x2,1000)


INFO: Float64
  0.175295 seconds (58.44 k allocations: 2.966 MB)

In [8]:
x4 = complex(x2)
info(eltype(x4))
@time run(x4,1000)


INFO: Complex{Float64}
  0.159062 seconds (59.26 k allocations: 3.005 MB)

In [ ]: