Efficient calculations in the loops

TO SUPPORT MY WORK, ORDER A COMMERCIAL LICENSE
THANK YOU!

Tutorial Math Collection API spec Download

Below is the code for JAVA, the code for C# is almost identical.

Case 1: User expression in the loop + output

import org.mariuszgromada.math.mxparser.*;
...
Argument x = new Argument("x");
Expression e = new Expression("2sin(x)", x);
for (int i = 0; i <= 10; i++) {
	x.setArgumentValue(i/10.0);
	mXparser.consolePrintln("x = " + x.getArgumentValue() + ", " + e.getExpressionString() + " = " + e.calculate());
}
[mXparser-v.5.0.4] x = 0.0, 2sin(x) = 0.0
[mXparser-v.5.0.4] x = 0.1, 2sin(x) = 0.1996668332936563
[mXparser-v.5.0.4] x = 0.2, 2sin(x) = 0.39733866159012243
[mXparser-v.5.0.4] x = 0.3, 2sin(x) = 0.5910404133226791
[mXparser-v.5.0.4] x = 0.4, 2sin(x) = 0.778836684617301
[mXparser-v.5.0.4] x = 0.5, 2sin(x) = 0.958851077208406
[mXparser-v.5.0.4] x = 0.6, 2sin(x) = 1.1292849467900707
[mXparser-v.5.0.4] x = 0.7, 2sin(x) = 1.288435374475382
[mXparser-v.5.0.4] x = 0.8, 2sin(x) = 1.4347121817990456
[mXparser-v.5.0.4] x = 0.9, 2sin(x) = 1.5666538192549668
[mXparser-v.5.0.4] x = 1.0, 2sin(x) = 1.682941969615793

Case 2: User function in the loop + output

import org.mariuszgromada.math.mxparser.*;
...
Function f = new Function("f(i) = 2sin(i/10)");
for (int i = 0; i <= 10; i++) {
	mXparser.consolePrintln("i = " + i + ", f(i) = " + f.calculate(i));
}
[mXparser-v.5.0.4] i = 0, f(i) = 0.0
[mXparser-v.5.0.4] i = 1, f(i) = 0.1996668332936563
[mXparser-v.5.0.4] i = 2, f(i) = 0.39733866159012243
[mXparser-v.5.0.4] i = 3, f(i) = 0.5910404133226791
[mXparser-v.5.0.4] i = 4, f(i) = 0.778836684617301
[mXparser-v.5.0.4] i = 5, f(i) = 0.958851077208406
[mXparser-v.5.0.4] i = 6, f(i) = 1.1292849467900707
[mXparser-v.5.0.4] i = 7, f(i) = 1.288435374475382
[mXparser-v.5.0.4] i = 8, f(i) = 1.4347121817990456
[mXparser-v.5.0.4] i = 9, f(i) = 1.5666538192549668
[mXparser-v.5.0.4] i = 10, f(i) = 1.682941969615793

Case 3: User argument (dependent) in the loop + output

import org.mariuszgromada.math.mxparser.*;
...
Argument x = new Argument("x");
Argument y = new Argument("y = 2sin(x)", x);
for (int i = 0; i <= 10; i++) {
	x.setArgumentValue(i/10.0);
	mXparser.consolePrintln("x = " + x.getArgumentValue() + ", y = " + y.getArgumentValue());
}
[mXparser-v.5.0.4] x = 0.0, y = 0.0
[mXparser-v.5.0.4] x = 0.1, y = 0.1996668332936563
[mXparser-v.5.0.4] x = 0.2, y = 0.39733866159012243
[mXparser-v.5.0.4] x = 0.3, y = 0.5910404133226791
[mXparser-v.5.0.4] x = 0.4, y = 0.778836684617301
[mXparser-v.5.0.4] x = 0.5, y = 0.958851077208406
[mXparser-v.5.0.4] x = 0.6, y = 1.1292849467900707
[mXparser-v.5.0.4] x = 0.7, y = 1.288435374475382
[mXparser-v.5.0.4] x = 0.8, y = 1.4347121817990456
[mXparser-v.5.0.4] x = 0.9, y = 1.5666538192549668
[mXparser-v.5.0.4] x = 1.0, y = 1.682941969615793

Case 4: User expression in the loop – Performance

import org.mariuszgromada.math.mxparser.*;
...
Argument x = new Argument("x");
Expression e = new Expression("2sin(x)", x);
long startTimeMills = System.currentTimeMillis();
int numOfIter = 10000000;
for (double xv = 0; xv <= numOfIter; xv++) {
	x.setArgumentValue(xv);
	double result = e.calculate();
}
long endTimeMills = System.currentTimeMillis();
double computingTimeSec = (endTimeMills - startTimeMills)/1000.0;
int iterPerSec = (int)(numOfIter / computingTimeSec);
mXparser.consolePrintln("Case A: User Expression in the loop");
mXparser.consolePrintln("-----------------");
mXparser.consolePrintln("Number of iterations = " + numOfIter);
mXparser.consolePrintln("Computing time = " + computingTimeSec + "s");
mXparser.consolePrintln("Number of iterations / s = " + iterPerSec);
[mXparser-v.5.0.4] Case A: User Expression in the loop
[mXparser-v.5.0.4] -----------------
[mXparser-v.5.0.4] Number of iterations = 10000000
[mXparser-v.5.0.4] Computing time = 23.445s
[mXparser-v.5.0.4] Number of iterations / s = 426530

Case 5: Speed up calculations by turning off the smart rounding options

mXparser.disableCanonicalRounding();
mXparser.disableUlpRounding();
mXparser.disableAlmostIntRounding();
Argument x = new Argument("x");
Expression e = new Expression("2sin(x)", x);
long startTimeMills = System.currentTimeMillis();
int numOfIter = 10000000;
for (double xv = 0; xv <= numOfIter; xv++) {
	x.setArgumentValue(xv);
	double result = e.calculate();
}
long endTimeMills = System.currentTimeMillis();
double computingTimeSec = (endTimeMills - startTimeMills)/1000.0;
int iterPerSec = (int)(numOfIter / computingTimeSec);
mXparser.consolePrintln("Case A: User Expression in the loop");
mXparser.consolePrintln("-----------------");
mXparser.consolePrintln("Number of iterations = " + numOfIter);
mXparser.consolePrintln("Computing time = " + computingTimeSec + "s");
mXparser.consolePrintln("Number of iterations / s = " + iterPerSec);
[mXparser-v.5.0.4] Case A: User Expression in the loop
[mXparser-v.5.0.4] -----------------
[mXparser-v.5.0.4] Number of iterations = 10000000
[mXparser-v.5.0.4] Computing time = 10.987s
[mXparser-v.5.0.4] Number of iterations / s = 910166

Case 6: User function in the loop – Performance

import org.mariuszgromada.math.mxparser.*;
...
Function f = new Function("f(x) = 2sin(x)");
long startTimeMills = System.currentTimeMillis();
int numOfIter = 10000000;
for (double xv = 0; xv <= numOfIter; xv++) {
	double result = f.calculate(xv);
}
long endTimeMills = System.currentTimeMillis();
double computingTimeSec = (endTimeMills - startTimeMills)/1000.0;
int iterPerSec = (int)(numOfIter / computingTimeSec);
mXparser.consolePrintln("Case B: User Function in the loop");
mXparser.consolePrintln("-----------------");
mXparser.consolePrintln("Number of iterations = " + numOfIter);
mXparser.consolePrintln("Computing time = " + computingTimeSec + "s");
mXparser.consolePrintln("Number of iterations / s = " + iterPerSec);
[mXparser-v.5.0.4] Case B: User Function in the loop
[mXparser-v.5.0.4] -----------------
[mXparser-v.5.0.4] Number of iterations = 10000000
[mXparser-v.5.0.4] Computing time = 24.335s
[mXparser-v.5.0.4] Number of iterations / s = 410930

Case 7: User argument (dependent) in the loop – Performance

import org.mariuszgromada.math.mxparser.*;
...
Argument x = new Argument("x");
Argument y = new Argument("y = 2sin(x)", x);
long startTimeMills = System.currentTimeMillis();
int numOfIter = 10000000;
for (double xv = 0; xv <= numOfIter; xv++) {
	x.setArgumentValue(xv);
	double result = y.getArgumentValue();
}
long endTimeMills = System.currentTimeMillis();
double computingTimeSec = (endTimeMills - startTimeMills)/1000.0;
int iterPerSec = (int)(numOfIter / computingTimeSec);
mXparser.consolePrintln("Case C: User Argument (dependent) in the loop");
mXparser.consolePrintln("-----------------");
mXparser.consolePrintln("Number of iterations = " + numOfIter);
mXparser.consolePrintln("Computing time = " + computingTimeSec + "s");
mXparser.consolePrintln("Number of iterations / s = " + iterPerSec);
[mXparser-v.5.0.4] Case C: User Argument (dependent) in the loop
[mXparser-v.5.0.4] -----------------
[mXparser-v.5.0.4] Number of iterations = 10000000
[mXparser-v.5.0.4] Computing time = 23.588s
[mXparser-v.5.0.4] Number of iterations / s = 423944
Nuget – Package Manager

Install-Package MathParser.org-mXparser -Version 5.0.6

Nuget – .NET CLI

dotnet add package MathParser.org-mXparser --version 5.0.6

Nuget – Package Reference

<PackageReference Include="MathParser.org-mXparser" Version="5.0.6"/>

Maven – Dependency

<dependency>
<groupid>
org.mariuszgromada.math</groupid>
<artifactid>
MathParser.org-mXparser</artifactid>
<version>
5.0.6</version>
</dependency>

Maven – Gradle

implementation 'org.mariuszgromada.math:MathParser.org-mXparser:5.0.6'

Maven – Gradle (Kotlin)

implementation("org.mariuszgromada.math:MathParser.org-mXparser:5.0.6")

GitHub

git clone https://github.com/mariuszgromada/MathParser.org-mXparser

OTHER DOWNLOAD OPTIONS

Download latest release – v.5.0.6 Leonis: .NET bin onlyDownload latest release – v.5.0.6 Leonis: JAVA bin onlyDownload latest release – v.5.0.6 Leonis: bin + doc

NEWS FROM MATHPARSER.ORG
SOURCE CODE

Source code .zipSource code .tar.gz
View on GitHubMathSpace.pl

DONATION
Did you find the software useful?
Please consider donation 🙂
DONATE