Skip to content

Instantly share code, notes, and snippets.

@GordStephen
Last active November 2, 2015 00:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GordStephen/209eac0b2a0a1bee4215 to your computer and use it in GitHub Desktop.
Save GordStephen/209eac0b2a0a1bee4215 to your computer and use it in GitHub Desktop.
julia> type A{S<:AbstractArray}
data::S
end
julia> type B{T,N}
data::AbstractArray{T,N}
end
julia> type C{T}
data::AbstractMatrix{T}
end
julia> type D{T,N}
data::Array{T,N}
end
julia> type E{T}
data::Matrix{T}
end
julia> function test(x)
tmpA = A(x)
tmpB = B(x)
tmpC = C(x)
tmpD = D(x)
tmpE = E(x)
@time for i = 1:10^7; tmpA.data[1,1]; end
@time for i = 1:10^7; tmpB.data[1,1]; end
@time for i = 1:10^7; tmpC.data[1,1]; end
@time for i = 1:10^7; tmpD.data[1,1]; end
@time for i = 1:10^7; tmpE.data[1,1]; end
end
test (generic function with 1 method)
julia> function test2(x)
tmpA = A(x)
tmpB = B(x)
tmpC = C(x)
tmpD = D(x)
tmpE = E(x)
@time for i = 1:10^7; sum(tmpA.data); end
@time for i = 1:10^7; sum(tmpB.data); end
@time for i = 1:10^7; sum(tmpC.data); end
@time for i = 1:10^7; sum(tmpD.data); end
@time for i = 1:10^7; sum(tmpE.data); end
end
test2 (generic function with 1 method)
julia> x = randn(10,10);
julia> test(x)
0.009130 seconds
0.311626 seconds (10.00 M allocations: 152.606 MB, 9.83% gc time)
0.283041 seconds (10.00 M allocations: 152.588 MB, 2.81% gc time)
0.006136 seconds
0.006092 seconds
julia> test(x)
0.032918 seconds
0.344165 seconds (10.00 M allocations: 152.588 MB, 2.26% gc time)
0.280957 seconds (10.00 M allocations: 152.588 MB, 2.64% gc time)
0.006105 seconds
0.006092 seconds
julia> test(x)
0.032655 seconds
0.347223 seconds (10.00 M allocations: 152.588 MB, 2.26% gc time)
0.288612 seconds (10.00 M allocations: 152.588 MB, 2.60% gc time)
0.006276 seconds
0.006306 seconds
julia> test2(x)
0.290332 seconds
0.951569 seconds (10.00 M allocations: 152.588 MB, 1.06% gc time)
0.958516 seconds (10.00 M allocations: 152.588 MB, 0.79% gc time)
0.280691 seconds
0.279666 seconds
julia> test2(x)
0.361693 seconds
0.950392 seconds (10.00 M allocations: 152.588 MB, 0.80% gc time)
0.955537 seconds (10.00 M allocations: 152.588 MB, 0.80% gc time)
0.277672 seconds
0.278493 seconds
julia> test2(x)
0.377770 seconds
0.948726 seconds (10.00 M allocations: 152.588 MB, 0.81% gc time)
0.958683 seconds (10.00 M allocations: 152.588 MB, 0.80% gc time)
0.280095 seconds
0.279119 seconds
julia> x = randn(1000,10);
julia> test2(x)
27.034179 seconds
28.241048 seconds (10.00 M allocations: 152.588 MB, 0.04% gc time)
29.013901 seconds (10.00 M allocations: 152.588 MB, 0.02% gc time)
29.141071 seconds
28.600888 seconds
julia> @time sum(x)
0.000028 seconds (61 allocations: 3.672 KB)
86.11169337777784
julia> @time sum(x);
0.000025 seconds (5 allocations: 176 bytes)
julia> @time for i = 1:10^7; sum(x); end;
27.637227 seconds (10.00 M allocations: 152.588 MB, 0.04% gc time)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment