]> www.ginac.de Git - cln.git/blob - benchmarks/timebench1.cc
Avoid "suggest explicit braces to avoid ambiguous 'else'" warnings.
[cln.git] / benchmarks / timebench1.cc
1 // Benchmarks from the LiDIA home page
2
3 #include <cln/number.h>
4 #include <cln/io.h>
5 #include <cln/integer.h>
6 #include <cln/float.h>
7 #include <cln/float_io.h>
8 #include <cln/real.h>
9 #include <cln/real_io.h>
10 #include <cln/complex.h>
11 #include <cln/complex_io.h>
12 #include <cstdlib>
13 #include <cstring>
14 #include <cln/timing.h>
15 #include <iostream>
16 using namespace cln;
17 using namespace std;
18
19 // Timings on Linux i486 33 MHz, 1000 decimal places = 104 32-bit words.
20 // Function              LiDIA       Pari       CLISP         CLN
21 //  pi                               0.17 / 0   0.38 / 0      0.12 / 0
22 //  gamma                            7.51 / 0    --           3.75 / 0
23 //  e                                1.20       5.06 / 0.20   0.66 / 0.10
24 //  multiplication                   0.018      0.010         0.010
25 //  sqrt(3)                          0.051      0.012         0.01
26 //  exp(log(2))                      3.13       0.08 / 3.94   0.04
27 //  log(exp(2))                      3.07       4.93 / 2.75   1.34
28 //  sin(pi/3)                        1.53       2.98          0.58
29 //  cos(pi/3)                        1.59       2.16          0.58
30 //  arcsin(sqrt(3)/2)                4.24       2.22          1.26
31 //  arccos(sqrt(3)/2)                4.26       2.22          1.26
32 //  sinh(log(2))                     3.16       2.02          0.03
33 //  cosh(log(2))                     3.17       2.09          0.04
34 //  arsinh(pi)                       1.93       2.62          0.65
35 //  arcosh(pi)                       1.95       2.26          0.69
36 // (Versions: Pari 1.39, clisp-1996-07-22, cln-1996-11-17.)
37
38 // Timings on Solaris Sparc 20, 75 MHz, 1000 decimal places = 104 32-bit words.
39 // Function              LiDIA       Pari       CLISP         CLN
40 //  pi                   0.06 / 0    0.04 / 0                 0.028 / 0
41 //  gamma                1.98 / 0    1.26 / 0                 1.99 / 0
42 //  e                    0           0.15                     0.15
43 //  multiplication
44 //  sqrt(3)              0.0025      0.01                     0.0025
45 //  exp(log(2))          0.25        0.39                     0.010
46 //  log(exp(2))          0.21        0.39                     0.31
47 //  sin(pi/3)            0.071       0.18                     0.13
48 //  cos(pi/3)            0.070       0.19                     0.13
49 //  arcsin(sqrt(3)/2)    0.30        0.55                     0.27
50 //  arccos(sqrt(3)/2)    0.30        0.55                     0.27
51 //  sinh(log(2))         0.25        0.41                     0.010
52 //  cosh(log(2))         0.25        0.40                     0.010
53 //  arsinh(pi)           0.16        0.26                     0.144
54 //  arcosh(pi)           0.16        0.26                     0.153
55 // (Versions: Pari 1.39, LiDIA 1.2.1 with libI, cln-1996-10-13.)
56
57 int main (int argc, char * argv[])
58 {
59         int repetitions = 1;
60         if ((argc >= 3) && !strcmp(argv[1],"-r")) {
61                 repetitions = atoi(argv[2]);
62                 argc -= 2; argv += 2;
63         }
64         if (argc < 1)
65                 exit(1);
66
67         fprint(std::cerr, "Number of repetitions: ");
68         fprintdecimal(std::cerr, repetitions);
69         fprint(std::cerr, "\n");
70
71         float_format_t prec = float_format(1000);
72
73         fprint(std::cerr, "pi\n");
74         { cl_F p;
75           { CL_TIMING; p = pi(prec); }
76           { CL_TIMING;
77             for (int rep = repetitions; rep > 0; rep--)
78               { cl_F p = pi(prec); }
79           }
80           cout << p << endl << endl;
81         }
82
83         fprint(std::cerr, "gamma\n");
84         { cl_F p;
85           { CL_TIMING; p = eulerconst(prec); }
86           { CL_TIMING;
87             for (int rep = repetitions; rep > 0; rep--)
88               { cl_F p = eulerconst(prec); }
89           }
90           cout << p << endl << endl;
91         }
92
93         fprint(std::cerr, "e\n");
94         { cl_F p = exp1(prec);
95           { CL_TIMING;
96             for (int rep = repetitions; rep > 0; rep--)
97               { cl_F p = exp1(prec); }
98           }
99           cout << p << endl << endl;
100         }
101
102         fprint(std::cerr, "sqrt(3)\n");
103         { cl_R p = sqrt(cl_float(3,prec));
104           { CL_TIMING;
105             for (int rep = repetitions; rep > 0; rep--)
106               { cl_R p = sqrt(cl_float(3,prec)); }
107           }
108           cout << p << endl << endl;
109         }
110
111         fprint(std::cerr, "exp(log(2))\n");
112         { cl_N p = exp(log(cl_float(2,prec)));
113           { CL_TIMING;
114             for (int rep = repetitions; rep > 0; rep--)
115               { cl_N p = exp(log(cl_float(2,prec))); }
116           }
117           cout << p << endl << endl;
118         }
119
120         fprint(std::cerr, "log(exp(2))\n");
121         { cl_N p = log(exp(cl_float(2,prec)));
122           { CL_TIMING;
123             for (int rep = repetitions; rep > 0; rep--)
124               { cl_N p = log(exp(cl_float(2,prec))); }
125           }
126           cout << p << endl << endl;
127         }
128
129         fprint(std::cerr, "sin(pi/3)\n");
130         { cl_R p = sin(pi(prec)/3);
131           { CL_TIMING;
132             for (int rep = repetitions; rep > 0; rep--)
133               { cl_R p = sin(pi(prec)/3); }
134           }
135           cout << p << endl << endl;
136         }
137
138         fprint(std::cerr, "cos(pi/3)\n");
139         { cl_R p = cos(pi(prec)/3);
140           { CL_TIMING;
141             for (int rep = repetitions; rep > 0; rep--)
142               { cl_R p = cos(pi(prec)/3); }
143           }
144           cout << p << endl << endl;
145         }
146
147         fprint(std::cerr, "arcsin(sqrt(3)/2)\n");
148         { cl_N p = asin(sqrt(cl_float(3,prec))/2);
149           { CL_TIMING;
150             for (int rep = repetitions; rep > 0; rep--)
151               { cl_N p = asin(sqrt(cl_float(3,prec))/2); }
152           }
153           cout << p << endl << endl;
154         }
155
156         fprint(std::cerr, "arccos(sqrt(3)/2)\n");
157         { cl_N p = acos(sqrt(cl_float(3,prec))/2);
158           { CL_TIMING;
159             for (int rep = repetitions; rep > 0; rep--)
160               { cl_N p = acos(sqrt(cl_float(3,prec))/2); }
161           }
162           cout << p << endl << endl;
163         }
164
165         fprint(std::cerr, "sinh(log(2))\n");
166         { cl_N p = sinh(log(cl_float(2,prec)));
167           { CL_TIMING;
168             for (int rep = repetitions; rep > 0; rep--)
169               { cl_N p = sinh(log(cl_float(2,prec))); }
170           }
171           cout << p << endl << endl;
172         }
173
174         fprint(std::cerr, "cosh(log(2))\n");
175         { cl_N p = cosh(log(cl_float(2,prec)));
176           { CL_TIMING;
177             for (int rep = repetitions; rep > 0; rep--)
178               { cl_N p = cosh(log(cl_float(2,prec))); }
179           }
180           cout << p << endl << endl;
181         }
182
183         fprint(std::cerr, "arsinh(pi)\n");
184         { cl_N p = asinh(pi(prec));
185           { CL_TIMING;
186             for (int rep = repetitions; rep > 0; rep--)
187               { cl_N p = asinh(pi(prec)); }
188           }
189           cout << p << endl << endl;
190         }
191
192         fprint(std::cerr, "arcosh(pi)\n");
193         { cl_N p = acosh(pi(prec));
194           { CL_TIMING;
195             for (int rep = repetitions; rep > 0; rep--)
196               { cl_N p = acosh(pi(prec)); }
197           }
198           cout << p << endl << endl;
199         }
200
201 }