muParser - A basic axample and benchmarks
Author: Ingo Berg
Description: Sample code for using muParser
Keywords: muparser, example, sample, benchmarks, benchmark, performance, comparison, release notes, changes, history

muParser - a fast math parser library
Version 2.1.0
My home
page
muParser
API-doc

### Introduction About the parser Release notes News Software using muParser Licence ### Features Features Overview Built in functions Built in binary operators Other operators ### Build instructions Building on Win32 Building on BSD/Linux Misc build instructions Including the files Compiler switches Where to ask for help ### Math parser interface Parser (de)initialization Evaluating an expression Single return value Multiple return values Bulk mode Setting the expression Defining identifier charsets Defining parser variables Explicit variable definition Implicit variable definition Defining constants Adding functionand operators Defining functions Defining operators Unary operators Binary operators Querying variables Querying constants Value recognition callbacks Removing variables/constants Localization Error handling Example code ### Current version Benchmarks | ## Example code If you put all this together, you get the source code for a small application. The application defines a parser variables ("`a`") and adds a user defined functions named "`MyFunc`". When using the parser make sure that you don't forget to catch the `Parser::exception_type` in your application. It contains detailed information helping you to find syntax errors in your formula.
    #include <iostream>
    #include "muParser.h"

    // Function callback
    double MySqr(double a_fVal)
    {
      return a_fVal*a_fVal;
    }

    // main program
    int main(int argc, char* argv[])
    {
      using namespace mu;

      try
      {
        double fVal = 1;
        Parser p;
        p.DefineVar("a", &fVal);
        p.DefineFun("MySqr", MySqr);
        p.SetExpr("MySqr(a)*_pi+min(10,a)");

        for (std::size_t a=0; a<100; ++a)
        {
          fVal = a;  // Change value of variable a
          std::cout << p.Eval() << std::endl;
        }
      }
      catch (Parser::exception_type &e)
      {
        std::cout << e.GetMsg() << std::endl;
      }
      return 0;
    }
## Benchmarks Finally, I'd like to give you some benchmarks. The benchmarking was done on an Intel Pentium P-4 with 2.6 GHz, with a version compiled by using MSVC++ 7.1 (Standard edition). The diagram shows number of evaluations per seconds vs. expression length. I compared both the static lib and the dll version with two other parsers that are freely available on the net, very fast and have a similar set of features. One of them is a commercial product. A higher curve means better performance. Expressions were created randomly. They used only `sin` and `cos` functions and contained multiple variables and constants. In order to smoothen the curves each point represents the value of a running average over 10 sample expressions.
© 2005-2011 Ingo Berg | ^ TOP