C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
exprtk_pgo.cpp
Go to the documentation of this file.
1/*
2 **************************************************************
3 * C++ Mathematical Expression Toolkit Library *
4 * *
5 * ExprTk PGO Suite *
6 * Author: Arash Partow (1999-2016) *
7 * URL: http://www.partow.net/programming/exprtk/index.html *
8 * *
9 * Copyright notice: *
10 * Free use of the Mathematical Expression Toolkit Library is *
11 * permitted under the guidelines and in accordance with the *
12 * most current version of the Common Public License. *
13 * http://www.opensource.org/licenses/cpl1.0.php *
14 * *
15 **************************************************************
16*/
17
18
19#include <cstdio>
20#include <fstream>
21#include <iostream>
22#include <string>
23#include <deque>
24
25#include "exprtk.hpp"
26
27
28static const double lower_bound_x = -100.0;
29static const double lower_bound_y = -100.0;
30static const double upper_bound_x = +100.0;
31static const double upper_bound_y = +100.0;
32static const double delta = 0.0111;
33
34
35template <typename Allocator,
36 template <typename,typename> class Sequence>
37inline std::size_t load_expressions(const std::string& file_name,
38 Sequence<std::string,Allocator>& sequence)
39{
40 std::ifstream stream(file_name.c_str());
41 if (!stream) return 0;
42 std::string buffer;
43 buffer.reserve(1024);
44 std::size_t line_count = 0;
45 while (std::getline(stream,buffer))
46 {
47 ++line_count;
48 sequence.push_back(buffer);
49 }
50 return line_count;
51}
52
53int main()
54{
55 std::deque<std::string> expr_str_list;
56
57 double x = 0.0;
58 double y = 0.0;
59 double z = 0.0;
60 double w = 0.0;
61
63 symbol_table.add_constants();
64 symbol_table.add_variable("x",x);
65 symbol_table.add_variable("y",y);
66 symbol_table.add_variable("z",z);
67 symbol_table.add_variable("w",w);
68
69 std::deque<exprtk::expression<double> > expression_list;
70
73
74 temp_expr.register_symbol_table(symbol_table);
75
76 {
77 static const std::size_t rounds = 10;
78 for (std::size_t r = 0; r < rounds; r++)
79 {
80 load_expressions("pgo_expression_list.txt",expr_str_list);
81
82 for (std::size_t i = 0; i < expr_str_list.size(); ++i)
83 {
84 if (!parser.compile(expr_str_list[i],temp_expr))
85 {
86 printf("[load_expression] - [%04d] Parser Error: %s\tExpression: %s\n",
87 i,
88 parser.error().c_str(),
89 expr_str_list[i].c_str());
90 }
91 else
92 expression_list.push_back(temp_expr);
93 }
94
95 std::cout << "Expression load: " << r << " Loaded: " << expression_list.size() << std::endl;
96 expr_str_list.clear();
97 expression_list.clear();
98 }
99 }
100
101 {
102 load_expressions("pgo_expression_list.txt",expr_str_list);
103
104 for (std::size_t i = 0; i < expr_str_list.size(); ++i)
105 {
106 if (!parser.compile(expr_str_list[i],temp_expr))
107 {
108 printf("[load_expression] - [%04d] Parser Error: %s\tExpression: %s\n",
109 i,
110 parser.error().c_str(),
111 expr_str_list[i].c_str());
112 }
113 else
114 expression_list.push_back(temp_expr);
115 }
116
117 static const std::size_t rounds = 100000;
118 double total = 0.0;
119
120 exprtk::timer timer;
121 timer.start();
122
123 x = 0.001;
124 y = 0.002;
125 z = 0.003;
126 w = 0.004;
127
128 for (std::size_t r = 0; r < rounds; r++)
129 {
130 for (std::size_t i = 0; i < expression_list.size(); ++i)
131 {
132 total += expression_list[i].value();
133 }
134
135 x += 0.01;
136 y += 0.02;
137 z += 0.03;
138 w += 0.04;
139 }
140
141 timer.stop();
142
143 if (0.0 != total)
144 {
145 printf("Total Time:%12.8fsec Rate:%15.5fevals/sec\n",
146 timer.time(),
147 (rounds * expression_list.size()) / timer.time());
148 }
149 }
150
151 return 0;
152}
void register_symbol_table(symbol_table< T > &st)
Definition exprtk.hpp:20992
bool compile(const std::string &expression_string, expression< T > &expr)
Definition exprtk.hpp:23584
std::string error() const
Definition exprtk.hpp:23858
bool add_variable(const std::string &variable_name, T &t, const bool is_constant=false)
Definition exprtk.hpp:20086
double time() const
Definition exprtk.hpp:41912
static const std::size_t rounds
static const double upper_bound_y
static const double upper_bound_x
static const double delta
std::size_t load_expressions(const std::string &file_name, Sequence< std::string, Allocator > &sequence)
static const double lower_bound_x
static const double lower_bound_y
int main()
static const std::string expression_list[]