COIN-OR::LEMON - Graph Library

Ticket #309: benchmark_graphs.2.cpp

File benchmark_graphs.2.cpp, 7.5 KB (added by Peter Kovacs, 15 years ago)

A nerw version of the benchmark test file

Line 
1#include <iostream>
2#include <fstream>
3#include <cstdlib>
4
5#include <lemon/list_graph.h>
6#include <lemon/smart_graph.h>
7#include <lemon/static_graph.h>
8#include <lemon/dimacs.h>
9#include <lemon/adaptors.h>
10#include <lemon/random.h>
11#include <lemon/time_measure.h>
12
13using namespace std;
14using namespace lemon;
15
16template <typename Graph>
17class NodeIterator1 {
18  const Graph &_gr;
19  int _cnt;
20public:
21  NodeIterator1(const Graph &gr) : _gr(gr), _cnt(0) {}
22  void operator()() {
23    for (typename Graph::NodeIt n(_gr); n != INVALID; ++n)
24      ++_cnt;
25  }
26  int count() { return _cnt; }
27};
28
29template <typename Graph>
30class NodeIterator2 {
31  const Graph &_gr;
32  int _cnt;
33public:
34  NodeIterator2(const Graph &gr) : _gr(gr), _cnt(0) {}
35  void operator()() {
36    typename Graph::Node n;
37    for (_gr.first(n); n != INVALID; _gr.next(n))
38      ++_cnt;
39  }
40  int count() { return _cnt; }
41};
42
43template <typename Graph>
44class ArcIterator1 {
45  const Graph &_gr;
46  int _cnt;
47public:
48  ArcIterator1(const Graph &gr) : _gr(gr), _cnt(0) {}
49  void operator()() {
50    for (typename Graph::ArcIt e(_gr); e != INVALID; ++e)
51      ++_cnt;
52  }
53  int count() { return _cnt; }
54};
55
56template <typename Graph>
57class ArcIterator2 {
58  const Graph &_gr;
59  int _cnt;
60public:
61  ArcIterator2(const Graph &gr) : _gr(gr), _cnt(0) {}
62  void operator()() {
63    typename Graph::Arc e;
64    for (_gr.first(e); e != INVALID; _gr.next(e))
65      ++_cnt;
66  }
67  int count() { return _cnt; }
68};
69
70template <typename Graph>
71class OutArcIterator1 {
72  const Graph &_gr;
73  int _cnt;
74public:
75  OutArcIterator1(const Graph &gr) : _gr(gr), _cnt(0) {}
76  void operator()() {
77    for (typename Graph::NodeIt n(_gr); n != INVALID; ++n) {
78      for (typename Graph::OutArcIt e(_gr, n); e != INVALID; ++e)
79        ++_cnt;
80    }
81  }
82  int count() { return _cnt; }
83};
84
85template <typename Graph>
86class OutArcIterator2 {
87  const Graph &_gr;
88  int _cnt;
89public:
90  OutArcIterator2(const Graph &gr) : _gr(gr), _cnt(0) {}
91  void operator()() {
92    typename Graph::Node n;
93    for (_gr.first(n); n != INVALID; _gr.next(n)) {
94      typename Graph::Arc e;
95      for (_gr.firstOut(e, n); e != INVALID; _gr.nextOut(e))
96        ++_cnt;
97    }
98  }
99  int count() { return _cnt; }
100};
101
102template <typename Graph>
103class InArcIterator1 {
104  const Graph &_gr;
105  int _cnt;
106public:
107  InArcIterator1(const Graph &gr) : _gr(gr), _cnt(0) {}
108  void operator()() {
109    for (typename Graph::NodeIt n(_gr); n != INVALID; ++n) {
110      for (typename Graph::InArcIt e(_gr, n); e != INVALID; ++e)
111        ++_cnt;
112    }
113  }
114  int count() { return _cnt; }
115};
116
117template <typename Graph>
118class InArcIterator2 {
119  const Graph &_gr;
120  int _cnt;
121public:
122  InArcIterator2(const Graph &gr) : _gr(gr), _cnt(0) {}
123  void operator()() {
124    typename Graph::Node n;
125    for (_gr.first(n); n != INVALID; _gr.next(n)) {
126      typename Graph::Arc e;
127      for (_gr.firstIn(e, n); e != INVALID; _gr.nextIn(e))
128        ++_cnt;
129    }
130  }
131  int count() { return _cnt; }
132};
133
134
135const int K = 10;
136
137template <typename GR>
138void benchmarkGraph(const GR &gr, int n, int m) {
139  Timer tni1, tni2, tai1, tai2, toai1, toai2, tiai1, tiai2;
140  NodeIterator1<GR> ni1(gr);
141  NodeIterator2<GR> ni2(gr);
142  ArcIterator1<GR> ai1(gr);
143  ArcIterator2<GR> ai2(gr);
144  OutArcIterator1<GR> oai1(gr);
145  OutArcIterator2<GR> oai2(gr);
146  InArcIterator1<GR> iai1(gr);
147  InArcIterator2<GR> iai2(gr);
148 
149  tni1.restart();
150  for (int i=0; i!=K; ++i) ni1();
151  tni1.stop();
152  tni2.restart();
153  for (int i=0; i!=K; ++i) ni2();
154  tni2.stop();
155  tai1.restart();
156  for (int i=0; i!=K; ++i) ai1();
157  tai1.stop();
158  tai2.restart();
159  for (int i=0; i!=K; ++i) ai2();
160  tai2.stop();
161  toai1.restart();
162  for (int i=0; i!=K; ++i) oai1();
163  toai1.stop();
164  toai2.restart();
165  for (int i=0; i!=K; ++i) oai2();
166  toai2.stop();
167  tiai1.restart();
168  for (int i=0; i!=K; ++i) iai1();
169  tiai1.stop();
170  tiai2.restart();
171  for (int i=0; i!=K; ++i) iai2();
172  tiai2.stop();
173
174  cout << "  NodeIt:    " << tni1.realTime() / K
175       << "    \t"        << tni2.realTime() / K << endl;
176  cout << "  ArcIt:     " << tai1.realTime() / K
177       << "    \t"        << tai2.realTime() / K << endl;
178  cout << "  OutArcIt:  " << toai1.realTime() / K
179       << "    \t"        << toai2.realTime() / K << endl;
180  cout << "  InArcIt:   " << tiai1.realTime() / K
181       << "    \t"        << tiai2.realTime() / K << endl;
182  cout << endl;
183 
184  if (ni1.count() != K * n) exit(-1);
185  if (ni2.count() != K * n) exit(-1);
186  if (ai1.count() != K * m) exit(-1);
187  if (ai2.count() != K * m) exit(-1);
188  if (oai1.count() != K * m) exit(-1);
189  if (oai2.count() != K * m) exit(-1);
190  if (iai1.count() != K * m) exit(-1);
191  if (iai2.count() != K * m) exit(-1);
192}
193
194int main(int argc, char *argv[])
195{
196  if (argc < 3) {
197    cout << "Usage: " << argv[0] << " n m" << endl;
198    return -1;
199  }
200 
201  int n = atoi(argv[1]);
202  int m = atoi(argv[2]);
203 
204  cout << "Benchmark tests on a random digraph (n = " << n
205       << ", m = " << m << ")" << endl << endl;
206 
207  vector<pair<int, int> > arcs1;
208  for (int j = 0; j != m; ++j) {
209    arcs1.push_back(make_pair(rnd[n], rnd[n]));
210  }
211  vector<pair<int, int> > arcs2(arcs1);
212  sort(arcs2.begin(), arcs2.end());
213
214  // Building graphs
215  cout << "ListDigraph - Building                  ";
216  Timer tlgr1;
217  ListDigraph lgr1;
218  vector<ListDigraph::Node> lnodes1;
219  for (int i = 0; i != n; ++i) {
220    lnodes1.push_back(lgr1.addNode());
221  } 
222  for (int j = 0; j != m; ++j) {
223    lgr1.addArc(lnodes1[arcs1[j].first], lnodes1[arcs1[j].second]);
224  }
225  tlgr1.stop();
226  cout << tlgr1.realTime() << endl;
227 
228  cout << "ListDigraph (sorted arcs) - Building    ";
229  Timer tlgr2;
230  ListDigraph lgr2;
231  vector<ListDigraph::Node> lnodes2;
232  for (int i = 0; i != n; ++i) {
233    lnodes2.push_back(lgr2.addNode());
234  } 
235  for (int j = 0; j != m; ++j) {
236    lgr2.addArc(lnodes2[arcs2[j].first], lnodes2[arcs2[j].second]);
237  }
238  tlgr2.stop();
239  cout << tlgr2.realTime() << endl;
240
241  cout << "SmartDigraph - Building                 ";
242  Timer tsgr1;
243  SmartDigraph sgr1;
244  vector<SmartDigraph::Node> snodes1;
245  for (int i = 0; i != n; ++i) {
246    snodes1.push_back(sgr1.addNode());
247  } 
248  for (int j = 0; j != m; ++j) {
249    sgr1.addArc(snodes1[arcs1[j].first], snodes1[arcs1[j].second]);
250  }
251  tsgr1.stop();
252  cout << tsgr1.realTime() << endl;
253
254  cout << "SmartDigraph (sorted arcs) - Building   ";
255  Timer tsgr2;
256  SmartDigraph sgr2;
257  vector<SmartDigraph::Node> snodes2;
258  for (int i = 0; i != n; ++i) {
259    snodes2.push_back(sgr2.addNode());
260  } 
261  for (int j = 0; j != m; ++j) {
262    sgr2.addArc(snodes2[arcs2[j].first], snodes2[arcs2[j].second]);
263  }
264  tsgr2.stop();
265  cout << tsgr2.realTime() << endl;
266 
267  cout << "StaticDigraph - Building                ";
268  Timer tstgr;
269  StaticDigraph stgr;
270  SmartDigraph::NodeMap<StaticDigraph::Node> nref(sgr1);
271  NullMap<SmartDigraph::Arc, StaticDigraph::Arc> aref;
272  stgr.build(sgr1, nref, aref);
273  tstgr.stop();
274  cout << tstgr.realTime() << endl;
275
276  cout << endl;
277/* 
278  // Iteration test
279  cout << "ListDigraph - Iteration" << endl;
280  benchmarkGraph(lgr1, n, m);
281 
282  cout << "ListDigraph (sorted arcs) - Iteration" << endl;
283  benchmarkGraph(lgr2, n, m);
284 
285  cout << "SmartDigraph - Iteration" << endl;
286  benchmarkGraph(sgr1, n, m);
287 
288  cout << "SmartDigraph (sorted arcs) - Iteration" << endl;
289  benchmarkGraph(sgr2, n, m);
290*/ 
291  cout << "StaticDigraph - Iteration" << endl;
292  benchmarkGraph(stgr, n, m);
293 
294  return 0;
295}