0

before any one mark this as duplicate please try to understand what i need
I have a software created in C# ASP.NET MVC5 were user can create any formula basically if..Else,switch condition which might include some custom function also from my custom DLL which include datediff,DatePart.
So for this scenario early we were using VBScript with the help of MSScriptControl but we decided to move for C# like syntax for formula.
i have explore much like CodeDom and Roslyn but the problem is it take almost AVG 130 ms for compile of 10 line of code. later i have also try to implement CS-Script which is much slow than above two because it internally uses CodeDOM,Roslyn,MONO as compiler option.
Now what i'm looking is alternate of VBScript which almost take 11 ms for compile and execute.

Please suggest me any other way or tool for this scenario.

How to improve compile time using CSharpCodeProvider

 `using Microsoft.CSharp;
  using System;
  using System.CodeDom.Compiler;`

  `namespace abc
  {
   public class Abc
   {
    static void Main()
    {
        var code = @"
   using System;
   namespace First
   {
    public class program
    {
     public static int Main()
     {
        if(Convert.ToDateTime(""01-04-2019 
         10:25:00"")>Convert.ToDateTime(""01-04-2019 15:00:00""))
         {
            return 1;
         }
         else
         {
            return 0;
          }
       }
    }
      }";
        Console.WriteLine(code);
        var options = new CompilerParameters();
        options.GenerateExecutable = false;
        options.GenerateInMemory = false;
        var provider = new CSharpCodeProvider();
        var compile = provider.CompileAssemblyFromSource(options, code);
        var type = compile.CompiledAssembly.GetType("First.program");
        var abc = Activator.CreateInstance(type);`

        `var method = type.GetMethod("Main");
        var result = method.Invoke(abc, null);`

        `Console.WriteLine(result); //output:`
      `}`
  `}`
  `}`
Mukesh Gupta
  • 59
  • 1
  • 7

1 Answers1

0

We did face some kind of similar problem in our projects. We were generating hundreds of small c# files and compile them one by one to get one function from each file (actually the file were in memory so we were not IO bound).

This operation was slow and we tried a lot of things to increase throughput (updating Roslyn, changing options, etc). We discovered that there is some overhead when starting a compilation which is not linear with the amount of code implied in it.

Our final solution which is so far the best we have (but we would really appreciate if someone has a better idea): group compilation. Calling the compiler on multiple files at once is much faster than calling it multiple times for the same amount of file.

What I would suggest in your case: group all your functions and compile them at once. 100ms for a single function is a pain when you have 1000 but 1s is ok for 1000 functions.

Bruno Belmondo
  • 2,299
  • 8
  • 18