|
| 1 | +use rust_data_structures::graph::{AdjacencyMatrix, Graph}; |
| 2 | +use std::hint::black_box; |
| 3 | +use std::time::{Duration, Instant}; |
| 4 | + |
| 5 | +const SIZE: i32 = 700; |
| 6 | + |
| 7 | +fn main() { |
| 8 | + println!("graph benchmark (manual, std::time::Instant)"); |
| 9 | + println!("size: {SIZE}"); |
| 10 | + println!( |
| 11 | + "adjacency list sparse build: {:?}", |
| 12 | + bench_list_sparse_build() |
| 13 | + ); |
| 14 | + println!( |
| 15 | + "adjacency matrix sparse build: {:?}", |
| 16 | + bench_matrix_sparse_build() |
| 17 | + ); |
| 18 | + println!("adjacency list edge query: {:?}", bench_list_edge_query()); |
| 19 | + println!( |
| 20 | + "adjacency matrix edge query: {:?}", |
| 21 | + bench_matrix_edge_query() |
| 22 | + ); |
| 23 | + println!( |
| 24 | + "adjacency list neighbor scan: {:?}", |
| 25 | + bench_list_neighbor_scan() |
| 26 | + ); |
| 27 | +} |
| 28 | + |
| 29 | +fn bench_list_sparse_build() -> Duration { |
| 30 | + let start = Instant::now(); |
| 31 | + black_box(build_list()); |
| 32 | + start.elapsed() |
| 33 | +} |
| 34 | + |
| 35 | +fn bench_matrix_sparse_build() -> Duration { |
| 36 | + let start = Instant::now(); |
| 37 | + black_box(build_matrix()); |
| 38 | + start.elapsed() |
| 39 | +} |
| 40 | + |
| 41 | +fn bench_list_edge_query() -> Duration { |
| 42 | + let graph = build_list(); |
| 43 | + |
| 44 | + let start = Instant::now(); |
| 45 | + for node in 0..SIZE { |
| 46 | + black_box(graph.has_edge(node, (node + 1) % SIZE)); |
| 47 | + } |
| 48 | + start.elapsed() |
| 49 | +} |
| 50 | + |
| 51 | +fn bench_matrix_edge_query() -> Duration { |
| 52 | + let graph = build_matrix(); |
| 53 | + |
| 54 | + let start = Instant::now(); |
| 55 | + for node in 0..SIZE { |
| 56 | + black_box(graph.has_edge(node, (node + 1) % SIZE)); |
| 57 | + } |
| 58 | + start.elapsed() |
| 59 | +} |
| 60 | + |
| 61 | +fn bench_list_neighbor_scan() -> Duration { |
| 62 | + let graph = build_list(); |
| 63 | + |
| 64 | + let start = Instant::now(); |
| 65 | + for node in 0..SIZE { |
| 66 | + black_box(graph.neighbors(node)); |
| 67 | + } |
| 68 | + start.elapsed() |
| 69 | +} |
| 70 | + |
| 71 | +fn build_list() -> Graph<i32> { |
| 72 | + let mut graph = Graph::new_directed(); |
| 73 | + |
| 74 | + for node in 0..SIZE { |
| 75 | + graph.add_node(node); |
| 76 | + } |
| 77 | + |
| 78 | + for node in 0..SIZE { |
| 79 | + graph.add_edge(node, (node + 1) % SIZE, 1).unwrap(); |
| 80 | + graph.add_edge(node, (node + 7) % SIZE, 1).unwrap(); |
| 81 | + } |
| 82 | + |
| 83 | + graph |
| 84 | +} |
| 85 | + |
| 86 | +fn build_matrix() -> AdjacencyMatrix<i32> { |
| 87 | + let mut graph = AdjacencyMatrix::new_directed(); |
| 88 | + |
| 89 | + for node in 0..SIZE { |
| 90 | + graph.add_node(node); |
| 91 | + } |
| 92 | + |
| 93 | + for node in 0..SIZE { |
| 94 | + graph.add_edge(node, (node + 1) % SIZE, 1).unwrap(); |
| 95 | + graph.add_edge(node, (node + 7) % SIZE, 1).unwrap(); |
| 96 | + } |
| 97 | + |
| 98 | + graph |
| 99 | +} |
0 commit comments