There is my old version codes:
struct A
a::Vector{Float64}
n::Int
end
test_func(a::A) = begin
a_vec,a_n = a.a,a.n
for i = 1 : size(a_vec,1)
a_vec[i] + a_n
end
end;
a1 = A(rand(1000_000),1)
a2 = A(rand(1000_000),2)
a3 = A(rand(1000_000),3);
a4 = A(rand(1000_000),4);
a5 = A(rand(1000_000),5);
# run it on a fresh session to find out the compilation time
@time begin
test_func(a1)
test_func(a2)
test_func(a3)
test_func(a4)
test_func(a5)
end;
0.002573 seconds
The there is my new version codes:
abstract type A end
struct A1 <: A
a::Vector{Float64}
end
struct A2 <: A
a::Vector{Float64}
end
struct A3 <: A
a::Vector{Float64}
end
struct A4 <: A
a::Vector{Float64}
end
struct A5 <: A
a::Vector{Float64}
end
test_func(a::T) where {T<:A1} = begin
for i = 1 : size(a.a,1)
a.a[i] + 1
end
end
test_func(a::T) where {T<:A2} = begin
for i = 1 : size(a.a,1)
a.a[i] + 1
end
end
test_func(a::T) where {T<:A3} = begin
for i = 1 : size(a.a,1)
a.a[i] + 1
end
end
test_func(a::T) where {T<:A4} = begin
for i = 1 : size(a.a,1)
a.a[i] + 1
end
end
test_func(a::T) where {T<:A5} = begin
for i = 1 : size(a.a,1)
a.a[i] + 1
end
end
a1 = A1(rand(1000_000))
a2 = A2(rand(1000_000))
a3 = A3(rand(1000_000));
a4 = A4(rand(1000_000));
a5 = A5(rand(1000_000));
# run it on a fresh session to find out the compilation time
@time begin
test_func(a1)
test_func(a2)
test_func(a3)
test_func(a4)
test_func(a5)
end;
0.020021 seconds (100.72 k allocations: 5.585 MiB, 99.78% compilation time)
As can be seen above, the new version code takes more than 10 times time to compile.
In my thought, the new code is more extentable than the old. So, how can I keep the style of new code and reduce the compilation time to the old?