]> www.ginac.de Git - ginac.git/blob - check/time_hashmap.cpp
[bugfix] ex::is_polynomial(x): fix miscomputations
[ginac.git] / check / time_hashmap.cpp
1 /** @file time_hashmap.cpp
2  *
3  *  Timings for exhashmap<> operations. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2011 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 "ginac.h"
24 #include "timer.h"
25 using namespace GiNaC;
26
27 #include <iostream>
28 #include <vector>
29 using namespace std;
30
31 template <class T>
32 static void run_timing(unsigned size, double &time_insert, double &time_find, double &time_erase)
33 {
34         vector<symbol> S;
35         T M;
36         timer t;
37
38         S.reserve(size);
39         for (unsigned i=0; i<size; ++i)
40                 S.push_back(symbol());
41
42         t.start();
43         for (unsigned i=0; i<size; ++i)
44                 M[S[i]] = S[(i+1)%size];
45         time_insert = t.read();
46
47         t.start();
48         for (unsigned i=0; i<size; ++i) {
49                 if (!M[S[i]].is_equal(S[(i+1)%size])) {
50                         clog << "map lookup failed" << endl;
51                         return;
52                 }
53         }
54         time_find = t.read();
55
56         t.start();
57         for (unsigned i=0; i<size; ++i) {
58                 if (M.erase(S[i]) != 1) {
59                         clog << "erasing element " << S[i] << " failed" << endl;
60                         return;
61                 }
62         }
63         if (!M.empty()) {
64                 clog << "map not empty (size = " << M.size() << ") after erasing all elements" << endl;
65                 return;
66         }
67         time_erase = t.read();
68 }
69
70
71 unsigned time_hashmap()
72 {
73         unsigned result = 0;
74
75         cout << "timing hash map operations" << flush;
76
77         unsigned s[] = {10000, 50000, 100000, 500000};
78         vector<unsigned> sizes(s, s+sizeof(s)/sizeof(*s));
79
80         vector<double> times_insert, times_find, times_erase;
81
82         for (vector<unsigned>::const_iterator i = sizes.begin(); i != sizes.end(); ++i) {
83                 double time_insert, time_find, time_erase;
84
85                 run_timing< exhashmap<ex> >(*i, time_insert, time_find, time_erase);
86
87 // If you like, you can compare it with this:
88 //              run_timing< std::map<ex, ex, ex_is_less> >(*i, time_insert, time_find, time_erase);
89
90                 times_insert.push_back(time_insert);
91                 times_find.push_back(time_find);
92                 times_erase.push_back(time_erase);
93                 cout << '.' << flush;
94         }
95
96         // print the report:
97         cout << endl << "          size:\t";
98         copy(sizes.begin(), sizes.end(), ostream_iterator<unsigned>(cout, "\t"));
99         cout << endl << "      insert/s:\t";
100         copy(times_insert.begin(), times_insert.end(), ostream_iterator<double>(cout, "\t"));
101         cout << endl << "        find/s:\t";
102         copy(times_find.begin(), times_find.end(), ostream_iterator<double>(cout, "\t"));
103         cout << endl << "       erase/s:\t";
104         copy(times_erase.begin(), times_erase.end(), ostream_iterator<double>(cout, "\t"));
105         cout << endl;
106
107         return result;
108 }
109
110 extern void randomify_symbol_serials();
111
112 int main(int argc, char** argv)
113 {
114         randomify_symbol_serials();
115         cout << setprecision(2) << showpoint;
116         return time_hashmap();
117 }