Exemple #1
0
        void form_load(Object _sender, EventArgs _e1)
        {
            try
            {
                _macroDefinition.SetParser(m_parser);

                // Define the variables and keep the keys to speed up value assignations
                m_parser.defineVar(m_x as IMTVariable);
                m_parser.defineVar(m_y as IMTVariable);
                m_parser.defineVar(m_z as IMTVariable);

                // Define a shared double variable for test purpose
                MTDouble v = new MTDouble();

                v.create("v", 1.0);
                m_parser.defineVar(v as IMTVariable);
                double r = 0;

                r = m_parser.evaluate("v");

                // Change the variable's value
                v.value = 10.0;

                r = m_parser.evaluate("v");
                Console.Out.WriteLine("r = {0}", r);

                // Create another parser and copy the existing parser configuration
                MTParser p2 = new MTParser();
                p2.copy(m_parser);

                r = p2.evaluate("v");

                // the shared variable has been copied
                // Changing the shared variable's value updates all parsers using this variable
                v.value = 3.0;
                r       = p2.evaluate("v");
                r       = m_parser.evaluate("v");

                // define a constant
                m_parser.defineConst("pi", 3.14159);

                // define  a user-defined function
                m_parser.defineFunc(new MySumFunction());

                // Load localized information
                string            dir       = System.AppDomain.CurrentDomain.BaseDirectory;
                MTParserLocalizer localizer = new MTParserLocalizer();
                localizer.locale = "en";
                localizer.registerAllLibraries(dir, "*.xml");


                sMTSyntax syntax = m_parser.getSyntax();
                syntax.decimalPoint      = '.';
                syntax.argumentSeparator = ',';
                m_parser.setSyntax(ref syntax);

                // Load plug-ins
                m_parser.loadAllPlugins(dir, "*.xml");
            }
            catch (Exception)
            {
                MessageBox.Show("Error: " + getLastExcepText(m_parser), "Project1");
            }
        }
Exemple #2
0
        void Benchmark_Click(Object _sender, EventArgs _e1)
        {
            try
            {
                // For optimum performance evaluate in a batch...

                // Redefine vector variables to contain multiple values
                MTParser benchParser = new MTParser();
                benchParser.copy(m_parser);

                MTDoubleVector x = new MTDoubleVector();
                MTDoubleVector y = new MTDoubleVector();
                MTDoubleVector z = new MTDoubleVector();
                x.create("x");
                y.create("y");
                z.create("z");

                benchParser.undefineAllVars();
                benchParser.defineVar(x as IMTVariable);
                benchParser.defineVar(y as IMTVariable);
                benchParser.defineVar(z as IMTVariable);

                // Compile the expression only once
                benchParser.compile(Expr.Text);

                // Generate random variable values...
                int nbEvals = 400000;

                double[]      xval = new double[nbEvals];
                double[]      yval = new double[nbEvals];
                double[]      zval = new double[nbEvals];
                System.Random r    = new System.Random(100);

                for (int i = 0; i < nbEvals; i += 1)
                {
                    xval[i] = r.Next(0, 1000);
                    yval[i] = r.Next(0, 1000);
                    zval[i] = r.Next(0, 1000);
                }

                // Set values...
                x.setValueVector(xval);
                y.setValueVector(yval);
                z.setValueVector(zval);


                double[] results = new double[nbEvals];                         // this will contain all the results after evaluations

                long beginTime = 0;
                long endTime   = 0;
                beginTime = GetTickCount();                                             // start the timer
                benchParser.evaluateCompiledBatch(nbEvals, results);                    // do all evaluations in one call, avoiding multiple COM calls
                endTime = GetTickCount();                                               // stop the timer

                long elapsedTime = 0;
                elapsedTime = endTime - beginTime;

                long   nbEvalSec   = 0;
                double timePerEval = 0;
                timePerEval = elapsedTime / (double)nbEvals;
                nbEvalSec   = (long)(nbEvals / elapsedTime * 1000.0);

                Msg.Text = "Nb. Evaluations: " + nbEvals;
                Msg.Text = Msg.Text + Environment.NewLine;
                Msg.Text = Msg.Text + "Elapsed time (ms): " + elapsedTime;
                Msg.Text = Msg.Text + Environment.NewLine;
                Msg.Text = Msg.Text + "Time per evaluation (ms): " + timePerEval;
                Msg.Text = Msg.Text + Environment.NewLine;
                Msg.Text = Msg.Text + "Nb. Eval per sec: " + nbEvalSec;
            }
            catch (Exception e)
            {
                Msg.Text = "Error: " + e.Message;
            }
        }
		void Benchmark_Click(Object _sender, EventArgs _e1) 
		{		
			
			try
			{
				// For optimum performance evaluate in a batch...

				// Redefine vector variables to contain multiple values 
				MTParser benchParser = new MTParser();
				benchParser.copy( m_parser );
				
				MTDoubleVector x = new MTDoubleVector();
				MTDoubleVector y = new MTDoubleVector();
				MTDoubleVector z = new MTDoubleVector();
				x.create("x");
				y.create("y");
				z.create("z");
                
				benchParser.undefineAllVars();
				benchParser.defineVar(x as IMTVariable);
				benchParser.defineVar(y as IMTVariable);
				benchParser.defineVar(z as IMTVariable);			

				// Compile the expression only once
				benchParser.compile(Expr.Text);				
				
				// Generate random variable values...
				int nbEvals = 400000;				

				double[] xval = new double[nbEvals];
				double[] yval = new double[nbEvals];
				double[] zval = new double[nbEvals];
				System.Random r = new System.Random(100);

				for(int i = 0; i < nbEvals; i += 1)
				{					
					xval[i] = r.Next(0, 1000);
					yval[i] = r.Next(0, 1000);
					zval[i] = r.Next(0, 1000);
				}
				
				// Set values...				
				x.setValueVector(xval);
				y.setValueVector(yval);
				z.setValueVector(zval);				
				
				
				double[] results = new double[nbEvals];		// this will contain all the results after evaluations
				
				long beginTime = 0;
				long endTime = 0;				
				beginTime = GetTickCount();								// start the timer
				benchParser.evaluateCompiledBatch(nbEvals, results);	// do all evaluations in one call, avoiding multiple COM calls							
				endTime = GetTickCount();								// stop the timer

				long elapsedTime = 0;
				elapsedTime = endTime - beginTime;				

				long nbEvalSec = 0;
				double timePerEval = 0;
				timePerEval = elapsedTime / (double)nbEvals;
				nbEvalSec = (long)(nbEvals / elapsedTime * 1000.0);

				Msg.Text = "Nb. Evaluations: " + nbEvals;
				Msg.Text = Msg.Text + Environment.NewLine ;
				Msg.Text = Msg.Text + "Elapsed time (ms): " + elapsedTime;
				Msg.Text = Msg.Text + Environment.NewLine ;
				Msg.Text = Msg.Text + "Time per evaluation (ms): " + timePerEval;
				Msg.Text = Msg.Text + Environment.NewLine ;
				Msg.Text = Msg.Text + "Nb. Eval per sec: " + nbEvalSec;
			}
			catch ( Exception e)
			{
				Msg.Text = "Error: " + e.Message;
			}
		}