# Problems with Optimising Function

I’m having a tough time optimising and parallelising this function that executes a simplex extrusion (solid modeling). Anything to make it faster would be great, even a couple macros.

``````function extrudeSimplicial(model::Lar.LAR, pattern)
V = [model[:,k] for k=1:size(model,2)]
FV = model
d, m = length(FV), length(pattern)
coords = collect(cumsum(append!(, abs.(pattern))))
offset, outcells, rangelimit, i = length(V), [], d*m, 0
for cell in FV
i += 1
tube = [v+k*offset for k in range(0, length=m+1) for v in cell]
cellTube = [tube[k:k+d] for k in range(1, length=rangelimit)]
if i==1 outcells = reshape(cellTube, d, m)
else outcells = vcat(outcells, reshape(cellTube, d, m)) end
end
cellGroups = []
for i in 1:size(outcells, 2)
if pattern[i]>0
cellGroups = vcat(cellGroups, outcells[:, i])
end
end
outVertices = [vcat(v, [z]) for z in coords for v in V]
cellGroups = convert(Array{Array{Int, 1}, 1}, cellGroups)
outModel = outVertices, cellGroups
hcat(outVertices...), cellGroups
end
function extrudeSimplicial(model::Union{Any,Lar.Cells}, pattern)
V,FV = model
d, m = length(FV), length(pattern)
coords = collect(cumsum(append!(, abs.(pattern))))
offset, outcells, rangelimit, i = length(V), [], d*m, 0
for cell in FV
i += 1
tube = [v+k*offset for k in range(0, length=m+1) for v in cell]
cellTube = [tube[k:k+d] for k in range(1, length=rangelimit)]
if i==1 outcells = reshape(cellTube, d, m)
else outcells = vcat(outcells, reshape(cellTube, d, m)) end
end
cellGroups = []
for i in 1:size(outcells, 2)
if pattern[i]>0
cellGroups = vcat(cellGroups, outcells[:, i])
end
end
outVertices = [vcat(v, [z]) for z in coords for v in V]
cellGroups = convert(Array{Array{Int, 1}, 1}, cellGroups)
outModel = outVertices, cellGroups
hcat(outVertices...), cellGroups
end
``````

Have you read the performance tips?

https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-tips

Some simple ones:

• `[:, k]` type indexing copies, you should probably use `@views`
• `[]` creates an untyped array, which is terrible for performance
• `hcat`/`vcat` can be slow when splatting large collections, you should probably use `reduce(vcat, ...)` instead
2 Likes

Yeah, I believe the most important section should be Performance Tips · The Julia Language w.r.t. profiling. @mauri5566: did you consider to give us some setup demonstrating calling your performance critical functions?