Coder Social home page Coder Social logo

ncalc2's Introduction

NCalc

Build status NuGet NuGet

โš ๏ธ 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 sklose#61

A clone of NCalc from http://ncalc.codeplex.com/ with the following changes:

  • added support for CoreCLR (.NET Standard 1.3+)
  • embedded portable version of Antlr (no extra library/dependency required)
  • added compilation of expressions to actual CLR lambdas

Installation

Simply install the package via NuGet

PM> Install-Package CoreCLR-NCalc

Creating Lambdas

Simple Expressions

var expr = new Expression("1 + 2");
Func<int> f = expr.ToLambda<int>();
Console.WriteLine(f()); // will print 3

Expressions with Functions and Parameters

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<ExpressionContext, bool> f = expr.ToLambda<ExpressionContext, bool>();

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%

ncalc2's People

Contributors

altso avatar andrewhanley2 avatar bradtglass avatar deltex80 avatar dgeller-ouhsc avatar eero-dev avatar fadulalla avatar kfrancis avatar lukas-ais avatar malukuseito avatar randbrown avatar rexcfnghk avatar sebastienros avatar sklose avatar tyrone-sudeium avatar victoriatolls avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.