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