In [14]:
## 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[14]:
colMeans (generic function with 3 methods)

In [16]:
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.109932 seconds (50.92 k allocations: 2.616 MB)

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

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


INFO: Float64
  3.513585 seconds (200.06 M allocations: 2.983 GB, 6.24% gc time)

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


INFO: Complex{Float64}
  5.286695 seconds (200.05 M allocations: 5.963 GB, 17.05% gc time)

In [ ]: