# Problems with Optimising Function

**URL:** <https://discourse.julialang.org/t/problems-with-optimising-function/81601>\
**Category:** Performance\
**Created:** [May 24, 2022, 4:01pm UTC](https://discourse.julialang.org/t/problems-with-optimising-function/81601 "2022-05-24T16:01:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mauri5566](https://avatars.discourse-cdn.com/v4/letter/m/e99b99/32.png) [@mauri5566](https://discourse.julialang.org/u/mauri5566)\
**Post date:** [May 24, 2022, 4:01pm UTC](https://discourse.julialang.org/t/problems-with-optimising-function/81601/1 "2022-05-24T16:01:41Z")

</div>

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.

```julia
function extrudeSimplicial(model::Lar.LAR, pattern)
    V = [model[1][:,k] for k=1:size(model[1],2)]
    FV = model[2]
    d, m = length(FV[1]), length(pattern)
    coords = collect(cumsum(append!([0], 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[1]), length(pattern)
    coords = collect(cumsum(append!([0], 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

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 24, 2022, 4:14pm UTC](https://discourse.julialang.org/t/problems-with-optimising-function/81601/2 "2022-05-24T16:14:46Z")

</div>

Have you read the performance tips?

[https://docs.julialang.org/en/v1/manual/performance-tips/#man-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

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [May 27, 2022, 6:05pm UTC](https://discourse.julialang.org/t/problems-with-optimising-function/81601/3 "2022-05-27T18:05:15Z")

</div>

> [@nilshg](#):
>
> Have you read the performance tips?

Yeah, I believe the most important section should be [Performance Tips · The Julia Language](https://docs.julialang.org/en/v1/manual/performance-tips/#tools) w.r.t. profiling. @mauri5566: did you consider to give us some setup demonstrating calling your performance critical functions?
