]> www.ginac.de Git - ginac.git/blob - doc/examples/compile1.cpp
[build] Define package version number in version.h (for non autotools build).
[ginac.git] / doc / examples / compile1.cpp
1 #include <ctime>
2 #include <iostream>
3 using namespace std;
4 #include <ginac/ginac.h>
5 using namespace GiNaC;
6
7 /*
8  * Demonstrates the use of compile_ex.
9  *
10  * Compiles a small expression as C code via compile_ex and evaluates the
11  * expression numerically. The evalation speed is timed and compared to the
12  * evaluation of the original GiNaC expression.
13  *
14  */
15
16 int main()
17 {
18         // Define some expression
19         symbol x("x");
20         ex expr = sin(x);
21  
22         // Some variables for timing
23         time_t start, end;
24         double cpu_time_used;
25
26         // Our function pointer that points to the compiled ex
27         FUNCP_1P fp;
28         compile_ex(expr, x, fp);
29
30         // Do some (not necessarily meaningful ;-)) numerical stuff ...
31
32         cout << "Doing numerics with compile_ex ..." << endl;
33
34         // First using compile_ex
35         {
36                 double result;
37                 double point = 0.2;
38                 start = clock();
39                 for (int i=0; i<100000; ++i) {
40                         point += 0.001;
41                         result += fp(point);
42                 }
43                 end = clock();
44
45                 // Show the result
46                 cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
47                 cout << "result = " << result << " in " << cpu_time_used << " seconds" << endl;
48         }
49
50         cout << "Doing numerics without compile_ex ..." << endl;
51
52         // Then without compile_ex
53         {
54                 ex result;
55                 ex point = 0.2;
56                 start = clock();
57                 for (int i=0; i<100000; ++i) {
58                         point += 0.001;
59                         result += sin(point);
60                 }
61                 end = clock();
62
63                 // Show the other result
64                 cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
65                 cout << "result = " << result << " in " << cpu_time_used << " seconds" << endl;
66         }
67
68         return 0;
69 }