https://github.com/sklose/ncalc2
expression evaluator for .NET with built-in compiler
https://github.com/sklose/ncalc2
c-sharp dotnet-core dotnet-standard dotnetcore expression-evaluator expression-parser
Last synced: about 1 year ago
JSON representation
expression evaluator for .NET with built-in compiler
- Host: GitHub
- URL: https://github.com/sklose/ncalc2
- Owner: sklose
- License: mit
- Created: 2016-01-20T16:53:09.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2024-09-29T05:07:44.000Z (over 1 year ago)
- Last Synced: 2024-10-30T03:43:08.386Z (over 1 year ago)
- Topics: c-sharp, dotnet-core, dotnet-standard, dotnetcore, expression-evaluator, expression-parser
- Language: C#
- Homepage:
- Size: 1.6 MB
- Stars: 170
- Watchers: 17
- Forks: 58
- Open Issues: 29
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# NCalc
[](https://ci.appveyor.com/project/sklose/ncalc2) [](https://www.nuget.org/packages/CoreCLR-NCalc) [](https://www.nuget.org/packages/CoreCLR-NCalc)
| :warning: This repository is currently only passively maintained. If there are fully fledged PRs, I will occasional merge those in and publish new versions. If you would like to become a maintainer and work on some of the open issues, please reply here https://github.com/sklose/NCalc2/issues/61
| --- |
A clone of NCalc from http://ncalc.codeplex.com/ with the following changes:
- added support for .NET Standard 2.0+
- added compilation of expressions to actual CLR lambdas
# Installation
Simply install the package via NuGet
```powershell
PM> Install-Package CoreCLR-NCalc
```
# Creating Lambdas
## Simple Expressions
```csharp
var expr = new Expression("1 + 2");
Func f = expr.ToLambda();
Console.WriteLine(f()); // will print 3
```
## Expressions with Functions and Parameters
```csharp
class ExpressionContext
{
public int Param1 { get; set; }
public string Param2 { get; set; }
public int Foo(int a, int b)
{
return a + b;
}
}
var expr = new Expression("Foo([Param1], 2) = 4 && [Param2] = 'test'");
Func f = expr.ToLambda();
var context = new ExpressionContext { Param1 = 2, Param2 = "test" };
Console.WriteLine(f(context)); // will print True
```
## Performance Comparison
The measurements were done during CI runs on AppVeyor and fluctuate a lot in between runs, but the speedup is consistently in the thousands of percent range. The speedup measured on actual hardware was even higher (between 10,000% and 35,000%).
| Formula | Description | Expression Evaluations / sec | Lambda Evaluations / sec | Speedup |
| ------------- | ------------- | ------------- | ------------- | ------------- |
| (4 * 12 / 7) + ((9 * 2) % 8) | Simple Arithmetics | 474,247.87 | 32,691,490.41 | 6,793.33% |
| 5 * 2 = 2 * 5 && (1 / 3.0) * 3 = 1 | Simple Arithmetics | 276,226.31 | 93,222,709.05 | 33,648.67% |
| [Param1] * 7 + [Param2] | Constant Values| 707,493.27 | 21,766,101.47 | 2,976.51% |
| [Param1] * 7 + [Param2] | Dynamic Values | 582,832.10 | 21,400,445.13 | 3,571.80% |
| Foo([Param1] * 7, [Param2]) | Dynamic Values and Function Call | 594,259.69 | 17,209,334.34 | 2,795.93% |