N
nkomli
My goal is to allow users to enter in an equation that will be
calculated. In order to do this I’ve attached a parser to the program.
Unfortunately the equation has to be calculated millions of times. For
example
while(count !=limit)
{
Xnext= x+1;
x=xnext;
}
The parser is called to recalculate the equation every cycle which I
believe is the reason is why the program is as slow as Christmas. I
was wondering if there was any way to store the equation once it is
parsed to allow it to run at least close to the speeds it would of the
equation was hardcoded.
Here is the relevant part of the code in this example assume args and
args2 are x+1 and y+1.
int calculate()
{
double i;
stx::AnyScalar x, y, xnext, ynext, val, val2;
x = 0;
y = 0;
// parse expression into a parse tree
stx:arseTree pt;
stx:arseTree pt2;
// evaluate the expression with a very simple symbol table
stx::BasicSymbolTable bst;
for (i = 1; i <= obj.icount; i++)//calculate equation (x+1) icount
times
{
//set the parser to recognize x and y as their new values every cycle
bst.setVariable("x", x);
bst.setVariable("y", y);
//call parser to evaluate functions contained within strings args and
args2
try
{
pt = stx:arseExpression(args);
val = pt.evaluate(bst);
pt2 = stx:arseExpression(args2);
val2 = pt2.evaluate(bst);
}
catch (stx::UnknownSymbolException &e)//exception handling
{
std::cout << "UnknownSymbolException: " << e.what() << "\n";
return 0;
}
catch (stx::ExpressionParserException &e)
{
std::cout << "ExpressionParserException: " << e.what() << "\n";
return 0;
}
x = val;
y = val2;
cout<< x << endl; //print x
}
return 0;
}
calculated. In order to do this I’ve attached a parser to the program.
Unfortunately the equation has to be calculated millions of times. For
example
while(count !=limit)
{
Xnext= x+1;
x=xnext;
}
The parser is called to recalculate the equation every cycle which I
believe is the reason is why the program is as slow as Christmas. I
was wondering if there was any way to store the equation once it is
parsed to allow it to run at least close to the speeds it would of the
equation was hardcoded.
Here is the relevant part of the code in this example assume args and
args2 are x+1 and y+1.
int calculate()
{
double i;
stx::AnyScalar x, y, xnext, ynext, val, val2;
x = 0;
y = 0;
// parse expression into a parse tree
stx:arseTree pt;
stx:arseTree pt2;
// evaluate the expression with a very simple symbol table
stx::BasicSymbolTable bst;
for (i = 1; i <= obj.icount; i++)//calculate equation (x+1) icount
times
{
//set the parser to recognize x and y as their new values every cycle
bst.setVariable("x", x);
bst.setVariable("y", y);
//call parser to evaluate functions contained within strings args and
args2
try
{
pt = stx:arseExpression(args);
val = pt.evaluate(bst);
pt2 = stx:arseExpression(args2);
val2 = pt2.evaluate(bst);
}
catch (stx::UnknownSymbolException &e)//exception handling
{
std::cout << "UnknownSymbolException: " << e.what() << "\n";
return 0;
}
catch (stx::ExpressionParserException &e)
{
std::cout << "ExpressionParserException: " << e.what() << "\n";
return 0;
}
x = val;
y = val2;
cout<< x << endl; //print x
}
return 0;
}