File: compile1.cpp

package info (click to toggle)
ginac 1.8.9-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 6,640 kB
  • sloc: cpp: 49,195; sh: 5,402; makefile: 448; python: 193
file content (76 lines) | stat: -rw-r--r-- 1,688 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <ctime>
#include <iostream>
#ifdef IN_GINAC
#include "ginac.h"
#else
#include <ginac/ginac.h>
#endif
using namespace std;
using namespace GiNaC;

/*
 * Demonstrates the use of compile_ex.
 *
 * Compiles a small expression as C code via compile_ex and evaluates the
 * expression numerically. The evalation speed is timed and compared to the
 * evaluation of the original GiNaC expression.
 *
 */

int main()
{
	// Define some expression
	symbol x("x");
	ex expr = sin(x);
 
	// Some variables for timing
	time_t start, end;
	double cpu_time_used;

	// Our function pointer that points to the compiled ex
	FUNCP_1P fp;

	// Optionally, compile with custom compiler flags:
	// setenv("CXXFLAGS", "-O3 -fomit-frame-pointer -ffast-math", 1);
	compile_ex(expr, x, fp);

	// Do some (not necessarily meaningful ;-)) numerical stuff ...

	cout << "Doing numerics with compile_ex ..." << endl;

	// First using compile_ex
	{
		double result = 0.0;
		double point = 0.2;
		start = clock();
		for (int i=0; i<100000; ++i) {
			point += 0.001;
			result += fp(point);
		}
		end = clock();

		// Show the result
		cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
		cout << "result = " << result << " in " << cpu_time_used << " seconds" << endl;
	}

	cout << "Doing numerics without compile_ex ..." << endl;

	// Then without compile_ex
	{
		ex result = 0.0;
		ex point = 0.2;
		start = clock();
		for (int i=0; i<100000; ++i) {
			point += 0.001;
			result += sin(point);
		}
		end = clock();

		// Show the other result
		cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
		cout << "result = " << result << " in " << cpu_time_used << " seconds" << endl;
	}

	return 0;
}