How are you timing it?
Assigning a function result to a variable just gives it a name, so something weird is going on.
PS. Your variable type declaration does nothing at best, and at worst will slow down the code — if you get the type wrong, it will force a conversion. Just do edges_to_segments = get_edges_to_segments(G, positions)