File: serialize.rs

package info (click to toggle)
rust-petgraph 0.6.4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 5,260 kB
  • sloc: makefile: 25
file content (55 lines) | stat: -rw-r--r-- 1,723 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#![feature(test)]

#[cfg(all(feature = "serde-1",feature="stable_graph"))]
mod serialize {
    extern crate petgraph;
    extern crate test;

    use petgraph::prelude::*;
    use rand::Rng;
    use test::Bencher;

    const NUM_NODES: usize = 1_000_000;
    const NUM_EDGES: usize = 100_000;
    const NUM_HOLES: usize = 1_000_000;

    fn make_stable_graph() -> StableGraph<u32, u32> {
        let mut g = StableGraph::with_capacity(NUM_NODES + NUM_HOLES, NUM_EDGES);
        let indices: Vec<_> = (0..NUM_NODES + NUM_HOLES)
            .map(|i| g.add_node(i as u32))
            .collect();

        let mut rng = rand::thread_rng();
        g.extend_with_edges((0..NUM_EDGES).map(|_| {
            let first = rng.gen_range(0.. NUM_NODES + NUM_HOLES);
            let second = rng.gen_range(0.. NUM_NODES + NUM_HOLES - 1);
            let second = second + (second >= first) as usize;
            let weight: u32 = rng.gen();
            (indices[first], indices[second], weight)
        }));

        // Remove nodes to make the structure a bit more interesting
        while g.node_count() > NUM_NODES {
            let idx = rng.gen_range(0.. indices.len());
            g.remove_node(indices[idx]);
        }

        g
    }

    #[bench]
    fn serialize_stable_graph(bench: &mut Bencher) {
        let graph = make_stable_graph();
        bench.iter(|| bincode::serialize(&graph).unwrap());
    }

    #[bench]
    fn deserialize_stable_graph(bench: &mut Bencher) {
        let graph = make_stable_graph();
        let data = bincode::serialize(&graph).unwrap();
        bench.iter(|| {
            let graph2: StableGraph<u32, u32> = bincode::deserialize(&data).unwrap();
            graph2
        });
    }
}