You should put them in separate cells. Everytime you redefine the function, you force it to recompile.

```
julia> function get_edges_to_segments(
graph::AbstractGraph,
positions::Vector{GeometryBasics.Point{2,Float32}},
)
#=
edges_to_segments = Dict(
(e, i) => Segment(
Meshes.Point(positions[src(e)][1], positions[src(e)][2]),
Meshes.Point(positions[dst(e)][1], positions[dst(e)][2]),
) for (i, e) in enumerate(edges(graph))
) =#
edges_to_segments::Dict{Tuple{Graphs.SimpleGraphs.SimpleEdge{Int64}, Int64}, GeometryBasics.Line{2, Float32}} = Dict(
(e, i) => GeometryBasics.Line(
positions[src(e)],
positions[dst(e)],
) for (i, e) in enumerate(edges(graph))
)
return edges_to_segments
end
get_edges_to_segments (generic function with 1 method)
julia> @time result = get_edges_to_segments(G, vertpositions);
0.074429 seconds (37.90 k allocations: 2.752 MiB, 99.94% compilation time)
julia> @time result = get_edges_to_segments(G, vertpositions);
0.000032 seconds (11 allocations: 52.547 KiB)
julia> @time result = get_edges_to_segments(G, vertpositions);
0.000045 seconds (11 allocations: 52.547 KiB)
julia> function get_edges_to_segments(
graph::AbstractGraph,
positions::Vector{GeometryBasics.Point{2,Float32}},
)
#=
edges_to_segments = Dict(
(e, i) => Segment(
Meshes.Point(positions[src(e)][1], positions[src(e)][2]),
Meshes.Point(positions[dst(e)][1], positions[dst(e)][2]),
) for (i, e) in enumerate(edges(graph))
) =#
edges_to_segments::Dict{Tuple{Graphs.SimpleGraphs.SimpleEdge{Int64}, Int64}, GeometryBasics.Line{2, Float32}} = Dict(
(e, i) => GeometryBasics.Line(
positions[src(e)],
positions[dst(e)],
) for (i, e) in enumerate(edges(graph))
)
return edges_to_segments
end
get_edges_to_segments (generic function with 1 method)
julia> @time result = get_edges_to_segments(G, vertpositions);
0.077947 seconds (37.90 k allocations: 2.755 MiB, 99.93% compilation time)
julia> @time result = get_edges_to_segments(G, vertpositions);
0.000036 seconds (11 allocations: 52.547 KiB)
julia> @time result = get_edges_to_segments(G, vertpositions);
0.000032 seconds (11 allocations: 52.547 KiB)
```

The compilation occurs on the first execution. Alternatively you could use a `precompile`

statement.

```
precompile(get_edges_to_segments, (SimpleGraph{Int64}, Vector{Point{2, Float32}}))
```

Here’s an example:

```
julia> function get_edges_to_segments(
graph::AbstractGraph,
positions::Vector{GeometryBasics.Point{2,Float32}},
)
#=
edges_to_segments = Dict(
(e, i) => Segment(
Meshes.Point(positions[src(e)][1], positions[src(e)][2]),
Meshes.Point(positions[dst(e)][1], positions[dst(e)][2]),
) for (i, e) in enumerate(edges(graph))
) =#
edges_to_segments::Dict{Tuple{Graphs.SimpleGraphs.SimpleEdge{Int64}, Int64}, GeometryBasics.Line{2, Float32}} = Dict(
(e, i) => GeometryBasics.Line(
positions[src(e)],
positions[dst(e)],
) for (i, e) in enumerate(edges(graph))
)
return edges_to_segments
end
get_edges_to_segments (generic function with 1 method)
julia> precompile(get_edges_to_segments, (SimpleGraph{Int64}, Vector{Point{2, Float32}})) # code will get compiled here
true
julia> @time result = get_edges_to_segments(G, vertpositions);
0.000038 seconds (11 allocations: 52.547 KiB)
julia> @time result = get_edges_to_segments(G, vertpositions);
0.000038 seconds (11 allocations: 52.547 KiB)
```