Is there a way to call C# script files with better performance results?

我怕爱的太早我们不能终老 提交于 2019-12-23 04:43:10

问题


I am currently calling a C# script file (http://www.csscript.net) from a C# app in Visual Studio 2010 using this method:

var script = new AsmHelper(CSScript.Load(
             @"C:\temp\CSScriptTest\CSScriptTest\cstest.cs"));
script.Invoke("*.LoopTest");

But I'm noticing that the performance on this is about twice what it takes to call a similar IronPython script using this method:

var ironPythonRuntime = Python.CreateRuntime();
dynamic loadPython = ironPythonRuntime.UseFile(
                     @"C:\temp\IronPythonTest\IronPythonTest\pytest.py");
loadPython.LoopTest();

Is there a method to call a C# script that performs better?


回答1:


Here's another alternative; I don't actually know what the performance of CS-Script is, so I can't compare them, but you can use CompilerParameters.GenerateInMemory with the CodeDomProvider.CompileAssemblyFromFile Method and then invoke it. GenerateInMemory does just what it says, generates the assembly in the process's memory instead of writing it to the disk. You could also use CompileAssemblyFromSource and just use a streamreader to read everything in the .cs files.




回答2:


Here is the answer I got from Oleg Shilo (the creator of CS-Script):

Actually the performance is the strong point for using CS-Script.

When it comes to performance it is important to remember the difference between IronPython and CS-Script execution model. CS-Script (as well as Boo) is relying on CLR and it compiles the script before the execution and IronPython is relying on DLR and it interprets the scripts during the execution.

Thus with CS-Script scripts will always be some startup overhead associated with the compilation of the script. However after the script loaded (compiled) CS-Script demonstrated the best possible performance possible for the managed code. This is because at runtime the script is no longer a script but an ordinary assembly and the performance is identical to the non-scripted scenarios. Thus if you have multiple LoopTest calls in your test then the performance "picture" will be very different: the loading and the first LoopTest call will be relatively slow but the following calls of LoopTest will be lightning fast.

To assist with the startup overhead CS-Script is using a caching mechanism (enabled by default) and if the script is not changed since the last execution it is not recompiled again but the previous compilation result is reused.

Thus for the best performance:

  • Try (if possible) to avoid modifying the scripts between executions.
  • Use caching via CSScript.CacheEnabled = true (enabled by default)
  • The dispatch style of calls (Invoke(string methodName)) is OK for a single call but for multiple calls use either GetStaticMethod or even better AlignToInterface. The performance advantage is very significant as Invoke in contrast to all other conventions is relying on the Reflection. This is the profiling result for the performance.cs sample script


来源:https://stackoverflow.com/questions/12061530/is-there-a-way-to-call-c-sharp-script-files-with-better-performance-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!