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