Nothing there looks out of line. indexing into wghts
is a O(log n) operation, I believe. I can’t imagine this takes significantly longer than the code without the weights accessor.
Are you adding edges or doing any graph modification as part of your timed routines? Here’s some benchmark code that tests accessing the weights:
julia> g = LightGraphs.SimpleDiGraph(100_000, 5_000_000)
{100000, 5000000} directed simple Int64 graph
julia> s = SimpleWeightedDiGraph(g)
{100000, 5000000} directed simple Int64 graph with Float64 weights
julia> for e in edges(s)
add_edge!(s, src(e), dst(e), rand())
end
julia> a = [(src(e), dst(e)) for e in edges(s) if (src(e) + dst(e)) % 4 != 0]; # just filter arbitrarily, keeping order
julia> length(a)
3751196
julia> function foo(g, a)
w = weights(g)
n = 0.0
for i in a
n += w[i[1], i[2]]
end
return n
end
foo (generic function with 1 method)
julia> @benchmark foo(s, a)
BenchmarkTools.Trial:
memory estimate: 16 bytes
allocs estimate: 1
--------------
minimum time: 53.333 ms (0.00% GC)
median time: 53.550 ms (0.00% GC)
mean time: 53.568 ms (0.00% GC)
maximum time: 54.033 ms (0.00% GC)
--------------
samples: 94
evals/sample: 1
You can see that sequentially accessing an ordered subset (3.75 million) edges in a SimpleWeightedDigraph with 100k nodes and 5 million edges takes approximately 53 milliseconds on average.