The function has the purpose of labeling the groups of “interconnected” cells [two cells are connected if have a 1 and are side by side or one above the other].

I had tried a first crude algorithm that did the job around 200us.

As I refined and tweaked the code I reduced the time to a few us (3 / 4us).

Instead, I find myself not understanding the result of using the @btime macro in the following cases.

When the avriables are defined inside the archipelago function I get a time 120 times greater than when these variables are defined in the main.

This is the how.

I hope someone will explain to me why.

```
A=rand([1,0], 8,10)
a=copy(A);
julia> function arcipelago(a)
cidx=findall(>(0),a)
dcidx=Dictionary(cidx,fill(-1, length(cidx)))
CI=CartesianIndex.([(0,1), (1,0), (0,-1), (-1,0)])
i=1
c=findfirst(==(-1),dcidx)
while !isnothing(c)
cc=CartesianIndex{2}[]
ci=[c]
while !isempty(ci)
for j in ci
a[j]=i
dcidx[j]=1
for n in CI
x=j+n
if haskey(dcidx,x) && dcidx[x]==-1
dcidx[x]=0
push!(cc,x)
end
end
end
ci=cc
cc=CartesianIndex{2}[]
end
c=findfirst(==(-1),dcidx)
i+=1
end
a
end
arcipelago (generic function with 1 method)
julia> @btime arcipelago(a)
6.180 μs (72 allocations: 7.75 KiB)
8×10 Matrix{Int64}:
1 0 0 2 2 0 0 7 7 0
0 2 2 2 0 0 0 0 7 7
2 2 2 2 2 0 0 7 7 7
2 2 0 0 2 0 0 0 0 0
0 2 2 2 0 2 0 0 0 0
3 0 0 2 2 2 2 0 0 9
0 0 0 0 0 0 0 8 8 0
4 4 0 5 0 6 6 0 8 0
```

```
cidx=findall(>(0),a)
dcidx=Dictionary(cidx,fill(-1, length(cidx)))
CI=CartesianIndex.([(0,1), (1,0), (0,-1), (-1,0)])
julia> function arcipelago(a)
# cidx=findall(>(0),a)
# dcidx=Dictionary(cidx,fill(-1, length(cidx)))
# CI=CartesianIndex.([(0,1), (1,0), (0,-1), (-1,0)])
i=1
c=findfirst(==(-1),dcidx)
while !isnothing(c)
cc=CartesianIndex{2}[]
ci=[c]
while !isempty(ci)
for j in ci
a[j]=i
dcidx[j]=1
for n in CI
x=j+n
if haskey(dcidx,x) && dcidx[x]==-1
dcidx[x]=0
push!(cc,x)
end
end
end
ci=cc
cc=CartesianIndex{2}[]
end
c=findfirst(==(-1),dcidx)
i+=1
end
a
end
arcipelago (generic function with 1 method)
julia>
julia>
julia> @btime arcipelago(a)
53.179 ns (0 allocations: 0 bytes)
8×10 Matrix{Int64}:
1 0 0 2 2 0 0 7 7 0
0 2 2 2 0 0 0 0 7 7
2 2 2 2 2 0 0 7 7 7
2 2 0 0 2 0 0 0 0 0
0 2 2 2 0 2 0 0 0 0
3 0 0 2 2 2 2 0 0 9
0 0 0 0 0 0 0 8 8 0
4 4 0 5 0 6 6 0 8 0
```