]> www.ginac.de Git - ginac.git/blob - doc/examples/compile1.cpp
Change www.ginac.de urls to https.
[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
29         // Optionally, compile with custom compiler flags:
30         // setenv("CXXFLAGS", "-O3 -fomit-frame-pointer -ffast-math", 1);
31         compile_ex(expr, x, fp);
32
33         // Do some (not necessarily meaningful ;-)) numerical stuff ...
34
35         cout << "Doing numerics with compile_ex ..." << endl;
36
37         // First using compile_ex
38         {
39                 double result;
40                 double point = 0.2;
41                 start = clock();
42                 for (int i=0; i<100000; ++i) {
43                         point += 0.001;
44                         result += fp(point);
45                 }
46                 end = clock();
47
48                 // Show the result
49                 cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
50                 cout << "result = " << result << " in " << cpu_time_used << " seconds" << endl;
51         }
52
53         cout << "Doing numerics without compile_ex ..." << endl;
54
55         // Then without compile_ex
56         {
57                 ex result;
58                 ex point = 0.2;
59                 start = clock();
60                 for (int i=0; i<100000; ++i) {
61                         point += 0.001;
62                         result += sin(point);
63                 }
64                 end = clock();
65
66                 // Show the other result
67                 cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
68                 cout << "result = " << result << " in " << cpu_time_used << " seconds" << endl;
69         }
70
71         return 0;
72 }