]> www.ginac.de Git - ginac.git/blob - check/time_parser.cpp
G_numeric: fix evaluation with y == 1
[ginac.git] / check / time_parser.cpp
1 /** @file time_parser.cpp
2  *
3  *  Time the parser. */
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 <cstddef>
28 #include <cstdlib>
29 #include <iostream>
30 #include <sstream>
31 #include <stdexcept>
32 #include <string>
33 #include <vector>
34 using namespace std;
35
36 extern void randomify_symbol_serials();
37
38 /// make a string "1+x+2*x^2+...+n*x^n"
39 static string prepare_str(const unsigned n, const char x = 'x')
40 {
41         ostringstream s;
42         s << x;
43         for (unsigned i = 2; i < n; i++)
44                 s << '+' << i << '*' << x << '^' << i; 
45         return s.str();
46 }
47
48 static double benchmark_and_cmp(const string& srep)
49 {
50         parser the_parser;
51         timer RSD10;
52         RSD10.start();
53         ex e = the_parser(srep);
54         const double t = RSD10.read();
55         return t;
56 }
57
58 int main(int argc, char** argv)
59 {
60         cout << "timing GiNaC parser..." << flush;
61         randomify_symbol_serials();
62         unsigned n_min = 1024;
63         unsigned n_max = 32768;
64         if (argc > 1)
65                 n_max = atoi(argv[1]);
66
67         vector<double> times;
68         vector<unsigned> ns;
69         for (unsigned n = n_min; n <= n_max; n = n << 1) {
70                 string srep = prepare_str(n);
71                 const double t = benchmark_and_cmp(srep);
72                 times.push_back(t);
73                 ns.push_back(n);
74         }
75
76         cout << "OK" << endl;
77         cout << "# terms  time, s" << endl;
78         for (size_t i = 0; i < times.size(); i++)
79                 cout << " " << ns[i] << '\t' << times[i] << endl;
80         return 0;
81 }