]> www.ginac.de Git - ginac.git/blob - check/time_hashmap.cpp
Fixed warning on 64bit machines.
[ginac.git] / check / time_hashmap.cpp
1 /** @file time_hashmap.cpp
2  *
3  *  Timings for exhashmap<> operations. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2005 Johannes Gutenberg University Mainz, Germany
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  */
22
23 #include "times.h"
24
25 template <class T>
26 static void run_timing(unsigned size, double &time_insert, double &time_find, double &time_erase)
27 {
28         vector<symbol> S;
29         T M;
30         timer t;
31
32         S.reserve(size);
33         for (unsigned i=0; i<size; ++i)
34                 S.push_back(symbol());
35
36         t.start();
37         for (unsigned i=0; i<size; ++i)
38                 M[S[i]] = S[(i+1)%size];
39         time_insert = t.read();
40
41         t.start();
42         for (unsigned i=0; i<size; ++i) {
43                 if (!M[S[i]].is_equal(S[(i+1)%size])) {
44                         clog << "map lookup failed" << endl;
45                         return;
46                 }
47         }
48         time_find = t.read();
49
50         t.start();
51         for (unsigned i=0; i<size; ++i) {
52                 if (M.erase(S[i]) != 1) {
53                         clog << "erasing element " << S[i] << " failed" << endl;
54                         return;
55                 }
56         }
57         if (!M.empty()) {
58                 clog << "map not empty (size = " << M.size() << ") after erasing all elements" << endl;
59                 return;
60         }
61         time_erase = t.read();
62 }
63
64
65 unsigned time_hashmap()
66 {
67         unsigned result = 0;
68
69         cout << "timing hash map operations" << flush;
70         clog << "-------hash map operations:" << endl;
71
72         unsigned s[] = {10000, 50000, 100000, 500000};
73         vector<unsigned> sizes(s, s+sizeof(s)/sizeof(*s));
74
75         vector<double> times_insert, times_find, times_erase;
76
77         for (vector<unsigned>::const_iterator i = sizes.begin(); i != sizes.end(); ++i) {
78                 double time_insert, time_find, time_erase;
79
80                 run_timing< exhashmap<ex> >(*i, time_insert, time_find, time_erase);
81
82 // If you like, you can compare it with this:
83 //              run_timing< std::map<ex, ex, ex_is_less> >(*i, time_insert, time_find, time_erase);
84
85                 times_insert.push_back(time_insert);
86                 times_find.push_back(time_find);
87                 times_erase.push_back(time_erase);
88                 cout << '.' << flush;
89         }
90
91         if (!result) {
92                 cout << " passed ";
93                 clog << "(no output)" << endl;
94         } else {
95                 cout << " failed ";
96         }
97
98         // print the report:
99         cout << endl << "          size:\t";
100         copy(sizes.begin(), sizes.end(), ostream_iterator<unsigned>(cout, "\t"));
101         cout << endl << "      insert/s:\t";
102         copy(times_insert.begin(), times_insert.end(), ostream_iterator<double>(cout, "\t"));
103         cout << endl << "        find/s:\t";
104         copy(times_find.begin(), times_find.end(), ostream_iterator<double>(cout, "\t"));
105         cout << endl << "       erase/s:\t";
106         copy(times_erase.begin(), times_erase.end(), ostream_iterator<double>(cout, "\t"));
107         cout << endl;
108
109         return result;
110 }