Files
gonum/graph/path/bench_test.go
Dan Kortschak 3d5d9d4a35 graph/path: allow incremental Bellman-Ford search
The impact on the current implementation is negligible.

name                          old time/op    new time/op    delta
BellmanFordFrom/500_tenth-8     4.14ms ± 1%    4.11ms ± 1%  -0.73%  (p=0.001 n=10+9)
BellmanFordFrom/1000_tenth-8    16.3ms ± 1%    16.1ms ± 1%  -0.82%  (p=0.000 n=10+10)
BellmanFordFrom/2000_tenth-8    66.2ms ± 1%    60.9ms ± 0%  -8.08%  (p=0.000 n=10+9)
BellmanFordFrom/500_half-8      18.0ms ± 5%    16.9ms ± 1%  -6.28%  (p=0.000 n=10+9)
BellmanFordFrom/1000_half-8     68.5ms ± 2%    67.4ms ± 1%  -1.50%  (p=0.000 n=9+8)
BellmanFordFrom/2000_half-8      281ms ± 1%     276ms ± 1%  -1.45%  (p=0.000 n=9+10)
BellmanFordFrom/500_full-8      33.4ms ± 0%    33.0ms ± 1%  -1.29%  (p=0.000 n=9+9)
BellmanFordFrom/1000_full-8      137ms ± 1%     133ms ± 0%  -3.11%  (p=0.000 n=8+9)
BellmanFordFrom/2000_full-8      549ms ± 1%     546ms ± 1%    ~     (p=0.065 n=10+9)

name                          old alloc/op   new alloc/op   delta
BellmanFordFrom/500_tenth-8      142kB ± 0%     142kB ± 0%    ~     (p=0.110 n=10+9)
BellmanFordFrom/1000_tenth-8     284kB ± 0%     284kB ± 0%    ~     (p=0.382 n=10+10)
BellmanFordFrom/2000_tenth-8     624kB ± 0%     624kB ± 0%    ~     (p=0.956 n=10+10)
BellmanFordFrom/500_half-8       142kB ± 0%     142kB ± 0%    ~     (p=0.666 n=10+10)
BellmanFordFrom/1000_half-8      284kB ± 0%     284kB ± 0%    ~     (p=0.070 n=10+8)
BellmanFordFrom/2000_half-8      624kB ± 0%     624kB ± 0%    ~     (p=0.290 n=10+10)
BellmanFordFrom/500_full-8       142kB ± 0%     142kB ± 0%    ~     (p=0.234 n=9+10)
BellmanFordFrom/1000_full-8      284kB ± 0%     284kB ± 0%    ~     (p=0.051 n=10+9)
BellmanFordFrom/2000_full-8      624kB ± 0%     624kB ± 0%    ~     (p=0.790 n=9+10)

name                          old allocs/op  new allocs/op  delta
BellmanFordFrom/500_tenth-8      2.03k ± 0%     2.03k ± 0%    ~     (all equal)
BellmanFordFrom/1000_tenth-8     4.03k ± 0%     4.03k ± 0%    ~     (all equal)
BellmanFordFrom/2000_tenth-8     8.03k ± 0%     8.03k ± 0%    ~     (all equal)
BellmanFordFrom/500_half-8       2.03k ± 0%     2.03k ± 0%    ~     (p=0.294 n=10+8)
BellmanFordFrom/1000_half-8      4.03k ± 0%     4.03k ± 0%    ~     (all equal)
BellmanFordFrom/2000_half-8      8.03k ± 0%     8.03k ± 0%    ~     (all equal)
BellmanFordFrom/500_full-8       2.03k ± 0%     2.03k ± 0%    ~     (p=1.000 n=10+10)
BellmanFordFrom/1000_full-8      4.03k ± 0%     4.03k ± 0%    ~     (all equal)
BellmanFordFrom/2000_full-8      8.03k ± 0%     8.03k ± 0%    ~     (p=1.000 n=10+10)

The performance of the lazy implementation is a little worse and allocations
are a lot worse than the eager implementation. This reflects that the benchmark
is performed on a graph with a single connected component which gains no benefit
from the incremental approach but suffers the cost of repeated reallocation for
appends.

name                          new time/op    inc time/op    delta
BellmanFordFrom/500_tenth-8     4.11ms ± 1%    4.28ms ± 1%   +4.05%  (p=0.000 n=9+10)
BellmanFordFrom/1000_tenth-8    16.1ms ± 1%    16.0ms ± 6%     ~     (p=0.143 n=10+10)
BellmanFordFrom/2000_tenth-8    60.9ms ± 0%    62.8ms ± 0%   +3.13%  (p=0.000 n=9+10)
BellmanFordFrom/500_half-8      16.9ms ± 1%    17.5ms ± 1%   +3.97%  (p=0.000 n=9+10)
BellmanFordFrom/1000_half-8     67.4ms ± 1%    70.0ms ± 2%   +3.76%  (p=0.000 n=8+9)
BellmanFordFrom/2000_half-8      276ms ± 1%     285ms ± 0%   +2.91%  (p=0.000 n=10+10)
BellmanFordFrom/500_full-8      33.0ms ± 1%    35.5ms ± 5%   +7.40%  (p=0.000 n=9+10)
BellmanFordFrom/1000_full-8      133ms ± 0%     137ms ± 2%   +3.48%  (p=0.000 n=9+9)
BellmanFordFrom/2000_full-8      546ms ± 1%     592ms ± 6%   +8.42%  (p=0.000 n=9+10)

name                          new alloc/op   inc alloc/op   delta
BellmanFordFrom/500_tenth-8      142kB ± 0%     182kB ± 0%  +27.84%  (p=0.000 n=9+10)
BellmanFordFrom/1000_tenth-8     284kB ± 0%     363kB ± 0%  +27.94%  (p=0.000 n=10+10)
BellmanFordFrom/2000_tenth-8     624kB ± 0%     893kB ± 0%  +43.26%  (p=0.000 n=10+10)
BellmanFordFrom/500_half-8       142kB ± 0%     182kB ± 0%  +27.83%  (p=0.000 n=10+10)
BellmanFordFrom/1000_half-8      284kB ± 0%     363kB ± 0%  +27.90%  (p=0.000 n=8+10)
BellmanFordFrom/2000_half-8      624kB ± 0%     893kB ± 0%  +43.25%  (p=0.000 n=10+10)
BellmanFordFrom/500_full-8       142kB ± 0%     182kB ± 0%  +27.82%  (p=0.000 n=10+10)
BellmanFordFrom/1000_full-8      284kB ± 0%     363kB ± 0%  +27.89%  (p=0.000 n=9+10)
BellmanFordFrom/2000_full-8      624kB ± 0%     893kB ± 0%  +43.23%  (p=0.000 n=10+10)

name                          new allocs/op  inc allocs/op  delta
BellmanFordFrom/500_tenth-8      2.03k ± 0%     2.10k ± 0%   +3.50%  (p=0.000 n=8+8)
BellmanFordFrom/1000_tenth-8     4.03k ± 0%     4.12k ± 0%   +2.39%  (p=0.000 n=10+10)
BellmanFordFrom/2000_tenth-8     8.03k ± 0%     8.17k ± 0%   +1.77%  (p=0.000 n=10+10)
BellmanFordFrom/500_half-8       2.03k ± 0%     2.10k ± 0%   +3.49%  (p=0.000 n=8+10)
BellmanFordFrom/1000_half-8      4.03k ± 0%     4.12k ± 0%   +2.36%  (p=0.000 n=9+10)
BellmanFordFrom/2000_half-8      8.03k ± 0%     8.17k ± 0%   +1.77%  (p=0.000 n=8+10)
BellmanFordFrom/500_full-8       2.03k ± 0%     2.10k ± 0%   +3.50%  (p=0.000 n=10+10)
BellmanFordFrom/1000_full-8      4.03k ± 0%     4.12k ± 0%   +2.36%  (p=0.000 n=9+10)
BellmanFordFrom/2000_full-8      8.03k ± 0%     8.17k ± 0%   +1.76%  (p=0.000 n=10+10)
2021-01-07 06:43:24 +10:30

193 lines
5.2 KiB
Go

// Copyright ©2015 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package path
import (
"fmt"
"sync"
"testing"
"gonum.org/v1/gonum/graph"
"gonum.org/v1/gonum/graph/graphs/gen"
"gonum.org/v1/gonum/graph/simple"
"gonum.org/v1/gonum/graph/traverse"
)
var (
gnpUndirected_10_tenth = gnpUndirected(10, 0.1)
gnpUndirected_100_tenth = gnpUndirected(100, 0.1)
gnpUndirected_1000_tenth = gnpUndirected(1000, 0.1)
gnpUndirected_10_half = gnpUndirected(10, 0.5)
gnpUndirected_100_half = gnpUndirected(100, 0.5)
gnpUndirected_1000_half = gnpUndirected(1000, 0.5)
nswUndirected_10_2_2_2 = navigableSmallWorldUndirected(10, 2, 2, 2)
nswUndirected_10_2_5_2 = navigableSmallWorldUndirected(10, 2, 5, 2)
nswUndirected_100_5_10_2 = navigableSmallWorldUndirected(100, 5, 10, 2)
nswUndirected_100_5_20_2 = navigableSmallWorldUndirected(100, 5, 20, 2)
)
func gnpUndirected(n int, p float64) func() graph.Undirected {
var once sync.Once
var cache graph.Undirected
return func() graph.Undirected {
once.Do(func() {
g := simple.NewUndirectedGraph()
err := gen.Gnp(g, n, p, nil)
if err != nil {
panic(fmt.Sprintf("path: bad test: %v", err))
}
cache = g
})
return cache
}
}
func navigableSmallWorldUndirected(n, p, q int, r float64) func() graph.Undirected {
var once sync.Once
var cache graph.Undirected
return func() graph.Undirected {
once.Do(func() {
g := simple.NewUndirectedGraph()
err := gen.NavigableSmallWorld(g, []int{n, n}, p, q, r, nil)
if err != nil {
panic(fmt.Sprintf("path: bad test: %v", err))
}
cache = g
})
return cache
}
}
func manhattan(size int) func(x, y graph.Node) float64 {
return func(x, y graph.Node) float64 {
return manhattanBetween(coordinatesForID(x, size, size), coordinatesForID(y, size, size))
}
}
func coordinatesForID(n graph.Node, c, r int) [2]int {
id := n.ID()
if id >= int64(c*r) {
panic("out of range")
}
return [2]int{int(id) / r, int(id) % r}
}
// manhattanBetween returns the Manhattan distance between a and b.
func manhattanBetween(a, b [2]int) float64 {
var d int
for i, v := range a {
d += abs(v - b[i])
}
return float64(d)
}
func abs(a int) int {
if a < 0 {
return -a
}
return a
}
func BenchmarkAStarUndirected(b *testing.B) {
benchmarks := []struct {
name string
graph graph.Undirected
h Heuristic
}{
{"GNP Undirected 10 tenth", gnpUndirected_10_tenth(), nil},
{"GNP Undirected 100 tenth", gnpUndirected_100_tenth(), nil},
{"GNP Undirected 1000 tenth", gnpUndirected_1000_tenth(), nil},
{"GNP Undirected 10 half", gnpUndirected_10_half(), nil},
{"GNP Undirected 100 half", gnpUndirected_100_half(), nil},
{"GNP Undirected 1000 half", gnpUndirected_1000_half(), nil},
{"NSW Undirected 10 2 2 2", nswUndirected_10_2_2_2(), nil},
{"NSW Undirected 10 2 2 2 heuristic", nswUndirected_10_2_2_2(), manhattan(10)},
{"NSW Undirected 10 2 5 2", nswUndirected_10_2_5_2(), nil},
{"NSW Undirected 10 2 5 2 heuristic", nswUndirected_10_2_5_2(), manhattan(10)},
{"NSW Undirected 100 5 10 2", nswUndirected_100_5_10_2(), nil},
{"NSW Undirected 100 5 10 2 heuristic", nswUndirected_100_5_10_2(), manhattan(100)},
{"NSW Undirected 100 5 20 2", nswUndirected_100_5_20_2(), nil},
{"NSW Undirected 100 5 20 2 heuristic", nswUndirected_100_5_20_2(), manhattan(100)},
}
for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {
var expanded int
for i := 0; i < b.N; i++ {
_, expanded = AStar(simple.Node(0), simple.Node(1), bm.graph, bm.h)
}
if expanded == 0 {
b.Fatal("unexpected number of expanded nodes")
}
})
}
}
var (
gnpDirected_500_tenth = gnpDirected(500, 0.1)
gnpDirected_1000_tenth = gnpDirected(1000, 0.1)
gnpDirected_2000_tenth = gnpDirected(2000, 0.1)
gnpDirected_500_half = gnpDirected(500, 0.5)
gnpDirected_1000_half = gnpDirected(1000, 0.5)
gnpDirected_2000_half = gnpDirected(2000, 0.5)
gnpDirected_500_full = gnpDirected(500, 1)
gnpDirected_1000_full = gnpDirected(1000, 1)
gnpDirected_2000_full = gnpDirected(2000, 1)
)
func gnpDirected(n int, p float64) func() graph.Directed {
var once sync.Once
var cache graph.Directed
return func() graph.Directed {
once.Do(func() {
g := simple.NewDirectedGraph()
err := gen.Gnp(g, n, p, nil)
if err != nil {
panic(fmt.Sprintf("path: bad test: %v", err))
}
cache = g
})
return cache
}
}
func BenchmarkBellmanFordFrom(b *testing.B) {
benchmarks := []struct {
name string
graph graph.Directed
}{
{"500 tenth", gnpDirected_500_tenth()},
{"1000 tenth", gnpDirected_1000_tenth()},
{"2000 tenth", gnpDirected_2000_tenth()},
{"500 half", gnpDirected_500_half()},
{"1000 half", gnpDirected_1000_half()},
{"2000 half", gnpDirected_2000_half()},
{"500 full", gnpDirected_500_full()},
{"1000 full", gnpDirected_1000_full()},
{"2000 full", gnpDirected_2000_full()},
}
type incremental struct {
traverse.Graph
}
for _, bm := range benchmarks {
for _, tg := range []struct {
typ string
g traverse.Graph
}{
{g: bm.graph},
{typ: " incremental", g: incremental{bm.graph}},
} {
b.Run(bm.name+tg.typ, func(b *testing.B) {
for i := 0; i < b.N; i++ {
BellmanFordFrom(bm.graph.Node(0), tg.g)
}
})
}
}
}