# Doubt about nested functions and scopes

**URL:** <https://discourse.julialang.org/t/doubt-about-nested-functions-and-scopes/17058>\
**Category:** General Usage\
**Created:** [November 1, 2018, 5:39pm UTC](https://discourse.julialang.org/t/doubt-about-nested-functions-and-scopes/17058 "2018-11-01T17:39:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gcalderone](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gcalderone/32/1539_2.png) [@gcalderone](https://discourse.julialang.org/u/gcalderone)\
**Post date:** [November 1, 2018, 5:39pm UTC](https://discourse.julialang.org/t/doubt-about-nested-functions-and-scopes/17058/1 "2018-11-01T17:39:36Z")

</div>

I need a `sortperm!` function which returns the number of comparisons actually performed to sort an array. I thought I could do this by intercepting and counting the calls to `isless`.

After a few trials I identified two possible solutions, implemented in the `mysortperm1!` and `mysortperm2!` functions shown below. Questions are:

- why `mysortperm1!` is much slower than `mysortperm2!` ?
- is there a better (i.e. faster) way to solve this problem?

```julia
function mysortperm1!(p, v)
    counter = 0
    sortperm!(p, v, lt=(a,b) -> (counter += 1; a < b))
    return counter
end

function mysortperm2!(p, v)
    counter = [0]
    sortperm!(p, v, lt=(a,b) -> (counter[1] += 1; a < b))
    return counter[1]
end

using BenchmarkTools
n = 100_000
v = rand(n);
p = zeros(Int, length(v));

@btime sortperm!(p, v);
@btime mysortperm1!(p, v)
@btime mysortperm2!(p, v)

```

Thanks

---

<div class="post-metadata">

**Author:** ![Daniel\_Berge](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@Daniel\_Berge](https://discourse.julialang.org/u/Daniel_Berge)\
**Post date:** [November 1, 2018, 6:47pm UTC](https://discourse.julialang.org/t/doubt-about-nested-functions-and-scopes/17058/2 "2018-11-01T18:47:49Z")

</div>

I’m not sure if I understand it fully myself, but I think it’s because the anonymous function `(a,b) -> (counter += 1; a < b)` is reaching outside it’s scope to modify an immutable, which being immutable it can’t, so it instead gets boxed.

```julia
let counter = 0
    (a,b) -> (counter += 1; a<b)
end |> dump

-> getfield(Main, Symbol("##83#84"))(Core.Box(0)) (function of type getfield(Main, Symbol("##83#84")))
    counter: Core.Box
      contents: Int64 0

```

Taking a look at the anonymous function, `counter` is captured and boxed, and a box is of type `Any` so the function is not type stable.

```julia
function mysortperm1!(p, v)
    counter = Ref(0)
    sortperm!(p, v, lt=(a,b) -> (counter[] += 1; a < b))
    return counter[]
end

```

As an alternative, you can make a reference to an immutable, and modify it by reference. Although someone may know of a better solution. Your `mysortperm2!` is modifying an array which is mutable and thus type stable, but has the unnecessary bounds checking overhead.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [November 1, 2018, 6:49pm UTC](https://discourse.julialang.org/t/doubt-about-nested-functions-and-scopes/17058/3 "2018-11-01T18:49:22Z")

</div>

I think the `Ref` way is the standard way to do it.
