File: time_hashmap.cpp

package info (click to toggle)
ginac 1.5.8-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 5,628 kB
  • ctags: 4,936
  • sloc: cpp: 44,703; sh: 11,126; perl: 1,157; yacc: 763; makefile: 414; lex: 200; sed: 32
file content (117 lines) | stat: -rw-r--r-- 3,257 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/** @file time_hashmap.cpp
 *
 *  Timings for exhashmap<> operations. */

/*
 *  GiNaC Copyright (C) 1999-2010 Johannes Gutenberg University Mainz, Germany
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "ginac.h"
#include "timer.h"
using namespace GiNaC;

#include <iostream>
#include <vector>
using namespace std;

template <class T>
static void run_timing(unsigned size, double &time_insert, double &time_find, double &time_erase)
{
	vector<symbol> S;
	T M;
	timer t;

	S.reserve(size);
	for (unsigned i=0; i<size; ++i)
		S.push_back(symbol());

	t.start();
	for (unsigned i=0; i<size; ++i)
		M[S[i]] = S[(i+1)%size];
	time_insert = t.read();

	t.start();
	for (unsigned i=0; i<size; ++i) {
		if (!M[S[i]].is_equal(S[(i+1)%size])) {
			clog << "map lookup failed" << endl;
			return;
		}
	}
	time_find = t.read();

	t.start();
	for (unsigned i=0; i<size; ++i) {
		if (M.erase(S[i]) != 1) {
			clog << "erasing element " << S[i] << " failed" << endl;
			return;
		}
	}
	if (!M.empty()) {
		clog << "map not empty (size = " << M.size() << ") after erasing all elements" << endl;
		return;
	}
	time_erase = t.read();
}


unsigned time_hashmap()
{
	unsigned result = 0;

	cout << "timing hash map operations" << flush;

	unsigned s[] = {10000, 50000, 100000, 500000};
	vector<unsigned> sizes(s, s+sizeof(s)/sizeof(*s));

	vector<double> times_insert, times_find, times_erase;

	for (vector<unsigned>::const_iterator i = sizes.begin(); i != sizes.end(); ++i) {
		double time_insert, time_find, time_erase;

		run_timing< exhashmap<ex> >(*i, time_insert, time_find, time_erase);

// If you like, you can compare it with this:
//		run_timing< std::map<ex, ex, ex_is_less> >(*i, time_insert, time_find, time_erase);

		times_insert.push_back(time_insert);
		times_find.push_back(time_find);
		times_erase.push_back(time_erase);
		cout << '.' << flush;
	}

	// print the report:
	cout << endl << "          size:\t";
	copy(sizes.begin(), sizes.end(), ostream_iterator<unsigned>(cout, "\t"));
	cout << endl << "      insert/s:\t";
	copy(times_insert.begin(), times_insert.end(), ostream_iterator<double>(cout, "\t"));
	cout << endl << "        find/s:\t";
	copy(times_find.begin(), times_find.end(), ostream_iterator<double>(cout, "\t"));
	cout << endl << "       erase/s:\t";
	copy(times_erase.begin(), times_erase.end(), ostream_iterator<double>(cout, "\t"));
	cout << endl;

	return result;
}

extern void randomify_symbol_serials();

int main(int argc, char** argv)
{
	randomify_symbol_serials();
	cout << setprecision(2) << showpoint;
	return time_hashmap();
}