]> www.ginac.de Git - ginac.git/blob - check/time_parser.cpp
[bugfix] parser::parse_literal_expr(): don't forget to consume the token...
[ginac.git] / check / time_parser.cpp
1 #include <iostream>
2 #include <string>
3 #include <sstream>
4 #include <cstddef>
5 #include <cstdlib>
6 #include <vector>
7 #include <stdexcept>
8 #include "ginac.h"
9 #include "timer.h"
10 extern void randomify_symbol_serials();
11 using namespace std;
12 using namespace GiNaC;
13
14 /// make a string "1+x+2*x^2+...+n*x^n"
15 static string prepare_str(const unsigned n, const char x = 'x')
16 {
17         ostringstream s;
18         s << x;
19         for (unsigned i = 2; i < n; i++)
20                 s << '+' << i << '*' << x << '^' << i; 
21         return s.str();
22 }
23
24 void benchmark_and_cmp(const string& srep, double& t_new, double& t_old)
25 {
26         parser the_parser;
27         timer RSD10;
28         RSD10.start();
29         ex e = the_parser(srep);
30         t_new = RSD10.read();
31         RSD10.stop();
32         if (t_new > 2.0)
33                 cout << '.' << flush;
34
35         symtab syms = the_parser.get_syms();
36         const symbol x = find_or_insert_symbol("x", syms, true);
37         lst sl;
38         sl = x;
39         RSD10.start();
40         ex e2(srep, sl);
41         t_old = RSD10.read();
42         
43         if (t_old > 2.0)
44                 cout << '.' << flush;
45
46         ex dif = (e - e2).expand();
47         if (!dif.is_zero()) {
48                 cerr << "Got a difference: " << dif << endl;
49                 throw std::logic_error("New and old parser give different results");
50         }
51 }
52
53 int main(int argc, char** argv)
54 {
55         cout << "timing GiNaC parser..." << flush;
56         randomify_symbol_serials();
57         unsigned n_min = 1024;
58         unsigned n_max = 32768;
59         if (argc > 1)
60                 n_max = atoi(argv[1]);
61
62         vector<double> times_new, times_old;
63         vector<unsigned> ns;
64         for (unsigned n = n_min; n <= n_max; n = n << 1) {
65                 double t_new, t_old;
66                 string srep = prepare_str(n);
67                 benchmark_and_cmp(srep, t_new, t_old);
68                 times_new.push_back(t_new);
69                 times_old.push_back(t_old);
70                 ns.push_back(n);
71         }
72
73         cout << "OK" << endl;
74         cout << "# terms  new parser, s  old parser, s" << endl;
75         for (size_t i = 0; i < times_new.size(); i++)
76                 cout << " " << ns[i] << '\t' << times_new[i] << '\t' << times_old[i] << endl;
77         return 0;
78 }