# How to speed the function H in this code? (Hausdorff Chirality Measurement)

**URL:** <https://discourse.julialang.org/t/how-to-speed-the-function-h-in-this-code-hausdorff-chirality-measurement/103831>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 13, 2023, 8:45pm UTC](https://discourse.julialang.org/t/how-to-speed-the-function-h-in-this-code-hausdorff-chirality-measurement/103831 "2023-09-13T20:45:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luis\_Manuel\_Espinoza](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luis_manuel_espinoza/32/36591_2.png) [@Luis\_Manuel\_Espinoza](https://discourse.julialang.org/u/Luis_Manuel_Espinoza)\
**Post date:** [September 13, 2023, 8:45pm UTC](https://discourse.julialang.org/t/how-to-speed-the-function-h-in-this-code-hausdorff-chirality-measurement/103831/1 "2023-09-13T20:45:33Z")

</div>

[Luis\_Manuel\_Espinoza](https://discourse.julialang.org/u/Luis_Manuel_Espinoza)

[1m](https://discourse.julialang.org/t/how-to-speed-the-function-h-in-this-code/103829)

I am trying to do the Hausdorff Chirality Measurement from the article by Andrzej B. Buda and Kurt Mislow\* but as I thought it is taking too long because to obtain a reliable value, I need to take “n” greater than 1,000,000 and it is with structures of 35 atoms It takes me almost 20 minutes, I was thinking about how to speed it up but I haven’t been able to make progress, I don’t know if anyone has an idea to speed it up, I would try to do it with parallel computing but I have no experience.

```julia
function Compute_of_CM_R_CCM(coords)
    # Compute the center of mass of the atoms
    n = size(coords, 1)
    center_of_mass = sum(coords, dims=1) / n
    # Compute the distances between the center of mass and each atom
    distances = zeros(n)
    for i in 1:n
        distances[i] = norm((center_of_mass.-coords)[i,:])
    end
    # Compute the radius of the smallest circle that encloses the atoms
    radius = maximum(distances)
    #Compute the coordinates translated to the center of mass
    coordinates_origin = coords .- center_of_mass
    
    return center_of_mass, radius, coordinates_origin
end

 function ρ(Q,Qp) 
    n = size(Q,1)
    sup = []
    for i in 1:n
        inf = []
        for j in 1:n
            dis = norm(Q[i,:].-Qp[j,:])
            push!(inf,dis)
        end
        push!(sup,minimum(inf))
    end
    return maximum(sup)    
end

function dQ(coordenadas)
    n = size(coordenadas,1)
    D = []
    for i in 1:n 
        distan = []
        for j in i+1:n
            d = norm(coordenadas[i,:]-coordenadas[j,:])
            push!(D,d)
        end
        end
    return maximum(D)
end

function H(q,qp,n)
    A = Compute_of_CM_R_CCM(q)[3]
    B = Compute_of_CM_R_CCM(qp)[3]
    HH = []
    for i in 1:n
        BB = B*qr(randn(3, 3)).Q
        HCM = max(ρ(A,BB),ρ(BB,A))/dQ(A)
        push!(HH,HCM)
    end
    return minimum(HH)
end

```

thanks for your time!

Sorry, I forgot to add an example of coordinates that are being used:

```julia
s = [ 1.73761 -2.3299 4.09897
   1.7145 -1.88395 2.6064
   0.810808 -3.53329 4.43137
   1.35042 -1.47855 4.72023
   2.18376 -2.67875 1.98346
   2.32905 -0.962011 2.49604
   3.08037 -3.63788 4.84995
   3.53686 -2.0441 5.09464
  -0.656357 -2.61487 3.56265
   3.11152 -2.69374 4.42536
   1.24236 -4.55928 4.94814
   2.0e-6 -1.53167 1.96559
  -0.51205 -3.38046 4.19649]

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 13, 2023, 9:20pm UTC](https://discourse.julialang.org/t/how-to-speed-the-function-h-in-this-code-hausdorff-chirality-measurement/103831/2 "2023-09-13T21:20:06Z")

</div>

There are many inefficiencies in your code, but I’ll point out two important ones:

Firstly,

> [@Luis\_Manuel\_Espinoza](#):
>
> ```julia
> sup = []
> inf = []
> D = []
> distan = []
> HH = []
> 
> ```

Make sure to never initialize vectors like this. These create a `Vector{Any}`, which means that the compiler has no way of knowing what kind of values are contained therein, and therefore cannot create fast specialized instructions.

Secondly,

> [@Luis\_Manuel\_Espinoza](#):
>
> ```julia
> function dQ(coordenadas)
> n = size(coordenadas,1)
> D = []
> for i in 1:n 
> distan = []
> for j in i+1:n
> d = norm(coordenadas[i,:]-coordenadas[j,:])
> push!(D,d)
> end
> end
> return maximum(D)
> end
> 
> ```

Here (and other places, too) you painstakingly build a vector, but in the end you throw it away, just keeping the maximum. Instead just keep the running max:

```julia
function dQ(coordenadas)
    n = size(coordenadas,1)
    dmax = 0.0
    for i in 1:n 
        for j in i+1:n
             # faster: d = @views norm(coordenadas[:,i]-coordenadas[:,j])
            d = norm(coordenadas[i,:]-coordenadas[j,:]) 
            dmax = max(dmax, d)
        end
    end
    return dmax
end

```

Always make sure to not create arrays if you don’t need them.

There’s a lot of other things as well, but those two struck me particularly.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 13, 2023, 9:23pm UTC](https://discourse.julialang.org/t/how-to-speed-the-function-h-in-this-code-hausdorff-chirality-measurement/103831/3 "2023-09-13T21:23:52Z")

</div>

BTW, if you want help to _really_ speed this up, please provide example input data, so that posters can run the code themselves. Right now it’s not easy to guess what sort of input data should be used.

---

<div class="post-metadata">

**Author:** ![Luis\_Manuel\_Espinoza](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luis_manuel_espinoza/32/36591_2.png) [@Luis\_Manuel\_Espinoza](https://discourse.julialang.org/u/Luis_Manuel_Espinoza)\
**Post date:** [September 13, 2023, 9:33pm UTC](https://discourse.julialang.org/t/how-to-speed-the-function-h-in-this-code-hausdorff-chirality-measurement/103831/4 "2023-09-13T21:33:20Z")

</div>

I’m sorry, I didn’t think it through

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 13, 2023, 9:35pm UTC](https://discourse.julialang.org/t/how-to-speed-the-function-h-in-this-code-hausdorff-chirality-measurement/103831/5 "2023-09-13T21:35:04Z")

</div>

Can you show how you call `H`? It takes three different input arguments, how does `s` relate to those?

---

<div class="post-metadata">

**Author:** ![Luis\_Manuel\_Espinoza](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luis_manuel_espinoza/32/36591_2.png) [@Luis\_Manuel\_Espinoza](https://discourse.julialang.org/u/Luis_Manuel_Espinoza)\
**Post date:** [September 13, 2023, 9:43pm UTC](https://discourse.julialang.org/t/how-to-speed-the-function-h-in-this-code-hausdorff-chirality-measurement/103831/6 "2023-09-13T21:43:03Z")

</div>

Ahhh the second argument could be ss =-1\*s and n is an Int that helps us increase the precision of the method, the function H is defined below the other functions above.

But thank you very much, the two observations you made previously have made me see those errors.
