One of the main concerns with performance, is getting out of the global scope. That is, do all the calculations within a function and avoid using global variables. It is pretty useless to optimize before doing this.
As an example, which might not be exactly what you want, here is a version of your code within a function, and with some changes I think would promote performance (it may not work correctly now, though - you should check and/or adapt your code with ideas from this):
function docalc()
A = sparse(
[
0.01 0 0 0 0 0 0 0 0 -0.001 0 0 0 0 0
0 0.01 0 0 0 0 0 0 0 0 -0.001 0 0 0 0
0 0 0.01 0 0 0 0 0 0 0 0 -0.001 0 0 0
0 0 0 1.0 0 0 -1.0 0 0 0 0 0 1.0 0 0
0 0 0 0 1.0 0 0 -1.0 0 0 0 0 0 1.0 0
0 0 0 0 0 1.0 0 0 -1.0 0 0 0 0 0 1.0
0 0 0 -1.0 0 0 1.003 -0.0007 -0.0007 0 0 0 0 0 0
0 0 0 0 -1.0 0 -0.0007 1.003 -0.0007 0 0 0 0 0 0
0 0 0 0 0 -1.0 -0.0007 -0.0007 1.00302 0 0 0 0 0 0
-0.001 0 0 0 0 0 0 0 0 0.004 -0.0007 -0.0007 0 0 0
0 -0.001 0 0 0 0 0 0 0 -0.0007 0.004 -0.0007 0 0 0
0 0 -0.001 0 0 0 0 0 0 -0.0007 -0.0007 0.004 0 0 0
0 0 0 1.0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 1.0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1.0 0 0 0 0 0 0 0 0 0
],
)
L = size(A, 1)
b = ones(L)
x = zeros(L)
p = [1, 2, 3, 11, 12, 10, 4, 5, 6, 7, 8, 9, 13, 14, 15]
q = [1, 2, 3, 11, 12, 10, 13, 14, 15, 7, 8, 9, 4, 5, 6]
invq = invperm(q)
edge = 6
A1 = A[p, q][begin:edge, begin:edge]
A2 = A[p, q][edge+1:end, edge+1:end]
b1 = b[p[begin:edge]]
b2 = b[p[edge+1:end]]
x1 = similar(b1)
x2 = similar(b2)
timeSim = 0:0.1:1
# timeSim = 0:0.0001:1
record = zeros(L, length(timeSim))
for (i, t) in enumerate(timeSim)
b1 .= b1 .* (1 + t) .+ t
b2 .= b2 .* (1 + t) .+ t
nonzeros(A1) .= nonzeros(A1) .* (1+t) .+ t
nonzeros(A2) .= nonzeros(A2) .* (1+t) .+ t
ldiv!(x1, lu(A1), b1)
ldiv!(x2, lu(A2), b2)
record[invq[begin:edge], i] .= x1
record[invq[edge+1:end], i] .= x2
end
return record
end
At one point it threw a Singular matrix error at me, but when doing a shorter loop, the function returned the record
matrix okay.