0

I am currently using this code:

using System;
using NCalc;

namespace SandboxConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            double[,] data = new double[100, 10];
            Random random = new Random();

            var numberOfRows = data.GetLength(0);
            var numberOfColumns = data.GetLength(1);

            for (int row = 0; row < numberOfRows; row++)
            {
                for (int col = 0; col < numberOfColumns; col++)
                {
                    data[row, col] = random.Next();
                }
            }

            // in the case of 10 columns the expression looks like: [x0] + [x1] + [x2] + [x3] + [x4] + [x5] + [x6] + [x7] + [x8] + [x9]
            var stringExpression = "";
            for (int col = 0; col < numberOfColumns - 1; col++)
            {
                stringExpression += string.Format("[x{0}] + ", col);
            }
            stringExpression += string.Format("[x{0}]", (numberOfColumns - 1));

            var exp = new Expression(stringExpression);
            var total = 0.0;

            for (int row = 0; row < numberOfRows; row++)
            {
                for (int col = 0; col < numberOfColumns; col++)
                {
                    exp.Parameters[string.Format("x{0}", col)] = data[row, col];
                }

                if (row % 100000 == 0)
                {
                    Console.WriteLine(row);
                }

                if (!exp.HasErrors())
                {
                    total += (double)exp.Evaluate();
                }
            }
        } 
    }
}

Here the fake 'dynamic' expression/formula:

[x0] + [x1] + [x2] + [x3] + [x4] + [x5] + [x6] + [x7] + [x8] + [x9]

adds 10 columns of a 10000000 row 'flat file'. The execution is not very fast and I hit limits, if I have lets say 100 million rows. Is there anything I can do to execute the above faster or should I use some other technologies to execute dynamically created formulas like this? Not sure how fast MySql would be - here I would generate the formula as SQL to the db via (e.g. Dapper).

cs0815
  • 16,751
  • 45
  • 136
  • 299
  • Did you compare this with perfomance of direct evaluation, without NCalc? Is direct perfomance good enough? – Evk Apr 04 '17 at 09:02
  • You would have to write a parser to do this. That's why I use NCalc. – cs0815 Apr 04 '17 at 10:32
  • Yes I understand, but I mean it's just interesting to know which effect NCalc has on this, so I asked how much faster this code will run without NCalc. – Evk Apr 04 '17 at 10:50
  • Ok will try to hardcode and check – cs0815 Apr 04 '17 at 11:53

0 Answers0