]> git.lizzy.rs Git - rust.git/blob - src/test/bench/core-map.rs
cf160ca31c6f185d3ae38b5a06ef6edda527674d
[rust.git] / src / test / bench / core-map.rs
1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 extern mod extra;
12
13 use extra::time;
14 use extra::treemap::TreeMap;
15 use std::hashmap::{HashMap, HashSet};
16 use std::io;
17 use std::os;
18 use std::rand::Rng;
19 use std::trie::TrieMap;
20 use std::uint;
21 use std::vec;
22
23 fn timed(label: &str, f: &fn()) {
24     let start = time::precise_time_s();
25     f();
26     let end = time::precise_time_s();
27     printfln!("  %s: %f", label, end - start);
28 }
29
30 fn ascending<M: MutableMap<uint, uint>>(map: &mut M, n_keys: uint) {
31     io::println(" Ascending integers:");
32
33     do timed("insert") {
34         for i in range(0u, n_keys) {
35             map.insert(i, i + 1);
36         }
37     }
38
39     do timed("search") {
40         for i in range(0u, n_keys) {
41             assert_eq!(map.find(&i).unwrap(), &(i + 1));
42         }
43     }
44
45     do timed("remove") {
46         for i in range(0, n_keys) {
47             assert!(map.remove(&i));
48         }
49     }
50 }
51
52 fn descending<M: MutableMap<uint, uint>>(map: &mut M, n_keys: uint) {
53     io::println(" Descending integers:");
54
55     do timed("insert") {
56         do uint::range_rev(n_keys, 0) |i| {
57             map.insert(i, i + 1);
58             true
59         };
60     }
61
62     do timed("search") {
63         do uint::range_rev(n_keys, 0) |i| {
64             assert_eq!(map.find(&i).unwrap(), &(i + 1));
65             true
66         };
67     }
68
69     do timed("remove") {
70         do uint::range_rev(n_keys, 0) |i| {
71             assert!(map.remove(&i));
72             true
73         };
74     }
75 }
76
77 fn vector<M: MutableMap<uint, uint>>(map: &mut M, n_keys: uint, dist: &[uint]) {
78
79     do timed("insert") {
80         for i in range(0u, n_keys) {
81             map.insert(dist[i], i + 1);
82         }
83     }
84
85     do timed("search") {
86         for i in range(0u, n_keys) {
87             assert_eq!(map.find(&dist[i]).unwrap(), &(i + 1));
88         }
89     }
90
91     do timed("remove") {
92         for i in range(0u, n_keys) {
93             assert!(map.remove(&dist[i]));
94         }
95     }
96 }
97
98 #[fixed_stack_segment]
99 fn main() {
100     let args = os::args();
101     let n_keys = {
102         if args.len() == 2 {
103             uint::from_str(args[1]).unwrap()
104         } else {
105             1000000
106         }
107     };
108
109     let mut rand = vec::with_capacity(n_keys);
110
111     {
112         let mut rng = std::rand::IsaacRng::new_seeded([1, 1, 1, 1, 1, 1, 1]);
113         let mut set = HashSet::new();
114         while set.len() != n_keys {
115             let next = rng.next() as uint;
116             if set.insert(next) {
117                 rand.push(next);
118             }
119         }
120     }
121
122     printfln!("%? keys", n_keys);
123
124     io::println("\nTreeMap:");
125
126     {
127         let mut map = TreeMap::new::<uint, uint>();
128         ascending(&mut map, n_keys);
129     }
130
131     {
132         let mut map = TreeMap::new::<uint, uint>();
133         descending(&mut map, n_keys);
134     }
135
136     {
137         io::println(" Random integers:");
138         let mut map = TreeMap::new::<uint, uint>();
139         vector(&mut map, n_keys, rand);
140     }
141
142     io::println("\nHashMap:");
143
144     {
145         let mut map = HashMap::new::<uint, uint>();
146         ascending(&mut map, n_keys);
147     }
148
149     {
150         let mut map = HashMap::new::<uint, uint>();
151         descending(&mut map, n_keys);
152     }
153
154     {
155         io::println(" Random integers:");
156         let mut map = HashMap::new::<uint, uint>();
157         vector(&mut map, n_keys, rand);
158     }
159
160     io::println("\nTrieMap:");
161
162     {
163         let mut map = TrieMap::new::<uint>();
164         ascending(&mut map, n_keys);
165     }
166
167     {
168         let mut map = TrieMap::new::<uint>();
169         descending(&mut map, n_keys);
170     }
171
172     {
173         io::println(" Random integers:");
174         let mut map = TrieMap::new::<uint>();
175         vector(&mut map, n_keys, rand);
176     }
177 }