https://github.com/JuliaLang/julia/blob/master/base/profile.jl
using ProfileView
function profile_test(n)
for i = 1:n
A = randn(100,100,20)
m = maximum(A)
Afft = fft(A)
Am = mapslices(sum, A, 2)
B = A[:,:,5]
Bsort = mapslices(sort, B, 1)
b = rand(100)
C = B.*b
end
end
profile_test(1) # run once to trigger compilation
Profile.clear() # in case we have any previous profiling data
@profile profile_test(10)
ProfileView.view()
@time profile_test(10)
@time profile_test(10)
@time profile_test(10)
elapsed time: 0.814026148 seconds (156743968 bytes allocated, 7.34% gc time) elapsed time: 0.814009174 seconds (156730208 bytes allocated, 7.58% gc time) elapsed time: 0.814474748 seconds (156730224 bytes allocated, 7.58% gc time)
float(A::AbstractArray) = map_promote(x->convert(FloatingPoint,x), A)
complex(A::AbstractArray) = map_promote(x->convert(Complex,x), A)
These anonymous functions look like trouble!
# test array of Int64 to convert
ir = [i for i in 1:1000000];
fr = float(ir);
cr = complex(ir);
@which float(ir)
@time float(ir);
@time float(ir);
@time float(ir);
@time float(ir);
elapsed time: 0.360642214 seconds (39991952 bytes allocated, 5.11% gc time) elapsed time: 0.365863059 seconds (39991968 bytes allocated, 6.51% gc time) elapsed time: 0.365807984 seconds (39991952 bytes allocated, 6.51% gc time) elapsed time: 0.366130502 seconds (39991952 bytes allocated, 6.52% gc time)
function Base.float(A::AbstractArray)
cnv(x) = convert(FloatingPoint,x)
Base.map_promote(cnv, A)
end
Warning: Method definition float(AbstractArray{T,N},)
float (generic function with 11 methods)
in module Base at abstractarray.jl:318 overwritten in module Main at In[8]:2.
@time float(ir);
@time float(ir);
@time float(ir);
@time float(ir);
elapsed time: 0.065522008 seconds (40043116 bytes allocated, 36.14% gc time) elapsed time: 0.038304734 seconds (39992448 bytes allocated) elapsed time: 0.062658875 seconds (39992448 bytes allocated, 38.22% gc time) elapsed time: 0.06216872 seconds (39992464 bytes allocated, 38.69% gc time)
0.36/0.062
5.806451612903226
@which complex(ir)
@time complex(ir);
@time complex(ir);
@time complex(ir);
@time complex(ir);
elapsed time: 0.406808621 seconds (55991984 bytes allocated, 5.58% gc time) elapsed time: 0.402890688 seconds (55991952 bytes allocated, 5.90% gc time) elapsed time: 0.438426556 seconds (55991952 bytes allocated, 5.54% gc time) elapsed time: 0.434572414 seconds (55991952 bytes allocated, 6.09% gc time)
function Base.complex(A::AbstractArray)
cnv(x) = convert(Complex,x)
Base.map_promote(cnv, A)
end
Warning: Method definition complex(AbstractArray{T,N},)
complex (generic function with 11 methods)
in module Base at abstractarray.jl:319 overwritten in module Main at In[13]:2.
@time complex(ir);
@time complex(ir);
@time complex(ir);
@time complex(ir);
elapsed time: 0.06833231 seconds (56044220 bytes allocated, 36.35% gc time) elapsed time: 0.064304898 seconds (55992448 bytes allocated, 37.81% gc time) elapsed time: 0.065242983 seconds (55992448 bytes allocated, 35.51% gc time) elapsed time: 0.064157927 seconds (55992448 bytes allocated, 37.61% gc time)
.40/.064
6.25