Rewritten heuristic and PRS GCD for univariate polynomials, added benchmark.
[ginac.git] / check / test_runner.h
1 #ifndef GINAC_CHECK_TEST_RUNNER_H
2 #define GINAC_CHECK_TEST_RUNNER_H
3 #include <iostream>
4 #include <cstdlib>
5 #include <string>
6 #include <ctime>
7 #include "timer.h"
8
9 template<typename T> static void 
10 run_benchmark(T& benchmark,
11               const unsigned ntests = 10,
12               const std::clock_t t_annoying = 15*CLOCKS_PER_SEC,
13               const std::clock_t t_min = CLOCKS_PER_SEC/100)
14 {
15         const std::clock_t start(std::clock());
16         std::clock_t elapsed(start);
17
18         timer P36;
19         unsigned n = 0;
20         double t = 0;
21         bool go_on = true;
22         do {
23                 ++n;
24                 P36.start();
25                 benchmark.run();
26                 t += P36.read();
27                 go_on = benchmark.check();
28                 if (!go_on)
29                         break;
30                 elapsed = std::clock() - start;
31                 if (elapsed > t_annoying)
32                         break;
33         } while (n <= ntests || elapsed < t_min);
34         t /= n;
35         benchmark.print_result(t);
36 }
37
38 // By default long-running timings are disabled (to not annoy the user).
39 // If the GINAC_RUN_EXPENSIVE_TIMINGS environment variable is set to "1",
40 // some of them (which are supposed to be relatively fast) will be enabled.
41 // If GINAC_RUN_EXPENSIVE_TIMINGS is set to "2", all timings are enabled.
42 static int run_expensive_timings_p()
43 {
44         static int value = 0;
45         static int cc = 0;
46         static const std::string env_name("GINAC_RUN_EXPENSIVE_TIMINGS");
47         if (cc++ == 0) {
48                 char* envvar = std::getenv(env_name.c_str());
49                 if (envvar != NULL) {
50                         value = std::atoi(envvar);
51                         if (value < 0 || value > 2)
52                                 value = 0;
53                 }
54                 if (value) {
55                         std::cerr << "WARNING: "
56                                 << "long-running timings are ENABLED."
57                                 << std::endl
58                                 << "Unset the \"" << env_name << "\" "
59                                 << "environment variable skip them."
60                                 << std::endl;
61                 }
62         }
63         return value;
64 }
65
66 #endif // GINAC_CHECK_TEST_RUNNER_H
67