C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
exprtk_funcall_benchmark.cpp
Go to the documentation of this file.
1/*
2 **************************************************************
3 * C++ Mathematical Expression Toolkit Library *
4 * *
5 * Function Call Benchmark (Custom vs Composite vs Native) *
6 * Author: Arash Partow (1999-2024) *
7 * URL: https://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 MIT License. *
13 * https://www.opensource.org/licenses/MIT *
14 * SPDX-License-Identifier: MIT *
15 * *
16 **************************************************************
17*/
18
19
20#include <cstdio>
21#include <string>
22
23#include "exprtk.hpp"
24
25
26template <typename T>
27struct func0 : public exprtk::ifunction<T>
28{
29 using exprtk::ifunction<T>::operator();
30
32 : exprtk::ifunction<T>(2)
33 {}
34
35 inline T operator()(const T& v1, const T& v2)
36 {
37 return T(1) + std::cos(v1 * v2) / T(3);
38 }
39};
40
41template <typename T>
42inline T func1(T v1, T v2)
43{
44 return T(1) + std::cos(v1 * v2) / T(3);
45}
46
47template <typename T>
49{
50 typedef exprtk::symbol_table<T> symbol_table_t;
51 typedef exprtk::expression<T> expression_t;
52 typedef exprtk::parser<T> parser_t;
53 typedef exprtk::function_compositor<T> compositor_t;
54 typedef typename compositor_t::function function_t;
55
56 T v1 = T(1);
57 T v2 = T(2);
58
59 func0<T> f0;
60
61 symbol_table_t symbol_table;
62
63 symbol_table.add_constants();
64
65 symbol_table.add_variable("v1" , v1 );
66 symbol_table.add_variable("v2" , v2 );
67 symbol_table.add_function("func0", f0 );
68 symbol_table.add_function("func1", func1<double>);
69
70 compositor_t compositor(symbol_table);
71
72 compositor.add(
73 function_t("func2")
74 .var("v1").var("v2")
75 .expression(" 1 + cos(v1 * v2) / 3;"));
76
77 const std::string program0 = "func0(v1,v2);";
78 const std::string program1 = "func1(v1,v2);";
79 const std::string program2 = "func2(v1,v2);";
80
81 expression_t expression0(symbol_table);
82 expression_t expression1(symbol_table);
83 expression_t expression2(symbol_table);
84
85 parser_t parser;
86
87 if (!parser.compile(program0,expression0))
88 {
89 printf("Error: %s\tExpression: %s\n",
90 parser.error().c_str(),
91 program0.c_str());
92
93 return;
94 }
95
96 if (!parser.compile(program1,expression1))
97 {
98 printf("Error: %s\tExpression: %s\n",
99 parser.error().c_str(),
100 program1.c_str());
101
102 return;
103 }
104
105 if (!parser.compile(program2,expression2))
106 {
107 printf("Error: %s\tExpression: %s\n",
108 parser.error().c_str(),
109 program1.c_str());
110
111 return;
112 }
113
114 const std::size_t rounds = 100000000;
115
116 {
117 T total = T(0);
118
119 exprtk::timer timer;
120 timer.start();
121
122 for (std::size_t r = 0; r < rounds; ++r)
123 {
124 total += expression0.value();
125 std::swap(v1,v2);
126 }
127
128 timer.stop();
129
130 printf("[custom function] Total time: %8.3fsec\tRate: %15.3f\tTotal:%20.5f\n",
131 timer.time(),
132 rounds / timer.time(),
133 total);
134 }
135
136 {
137 T total = T(0);
138
139 exprtk::timer timer;
140 timer.start();
141
142 for (std::size_t r = 0; r < rounds; ++r)
143 {
144 total += expression1.value();
145 std::swap(v1,v2);
146 }
147
148 timer.stop();
149
150 printf("[free function ] Total time: %8.3fsec\tRate: %15.3f\tTotal:%20.5f\n",
151 timer.time(),
152 rounds / timer.time(),
153 total);
154 }
155
156 {
157 T total = T(0);
158
159 exprtk::timer timer;
160 timer.start();
161
162 for (std::size_t r = 0; r < rounds; ++r)
163 {
164 total += expression2.value();
165 std::swap(v1,v2);
166 }
167
168 timer.stop();
169
170 printf("[compositor ] Total time: %8.3fsec\tRate: %15.3f\tTotal:%20.5f\n",
171 timer.time(),
172 rounds / timer.time(),
173 total);
174 }
175
176 {
177 T total = T(0);
178
179 exprtk::timer timer;
180 timer.start();
181
182 for (std::size_t r = 0; r < rounds; ++r)
183 {
184 total += func1<T>(v1,v2);
185 std::swap(v1,v2);
186 }
187
188 timer.stop();
189
190 printf("[native ] Total time: %8.3fsec\tRate: %15.3f\tTotal:%20.5f\n",
191 timer.time(),
192 rounds / timer.time(),
193 total);
194 }
195}
196
197int main()
198{
199 function_call_benchmark01<double>();
200 return 0;
201}
ifunction(const std::size_t &pc)
Definition exprtk.hpp:19545
double time() const
Definition exprtk.hpp:43502
static const std::size_t rounds
void function_call_benchmark01()
T func1(T v1, T v2)
T operator()(const T &v1, const T &v2)