有没有一种方法可以调用C#脚本文件,从而获得更好的性能结果? [英] Is there a way to call C# script files with better performance results?

查看:69
本文介绍了有没有一种方法可以调用C#脚本文件,从而获得更好的性能结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在从C#调用C#脚本文件( http://www.csscript.net )使用以下方法在Visual Studio 2010中运行应用程序:

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");

但是我注意到,此方法的性能大约是使用此方法调用类似的IronPython脚本所需的性能的两倍:

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();

有没有一种方法可以调用性能更好的C#脚本?

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

推荐答案

这是我从Oleg Shilo(CS-Script的创建者)得到的答案:

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

实际上,性能是使用CS-Script的强项.

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

在性能方面,重要的是要记住 IronPython和CS-Script执行模型之间的区别. CS脚本 (以及Boo)都依赖CLR,并且它会在编译脚本之前 执行,并且IronPython依赖DLR,它解释了 执行过程中的脚本.

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.

因此,使用CS-Script脚本将总是有些启动开销 与脚本的编译相关联.但是之后 加载(编译)CS脚本的脚本演示了尽可能的最佳方法 托管代码可能的性能.这是因为在运行时 该脚本不再是脚本,而是普通程序集和 性能与非脚本方案相同.因此,如果你 在您的测试中有多个LoopTest调用,然后性能 图片"将有很大的不同:加载和第一个LoopTest 调用将相对较慢,但随后的LoopTest调用将 快点闪电.

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.

为了帮助解决启动开销,CS-Script正在使用缓存 机制(默认情况下启用),并且如果脚本自 最后一次执行不会重新编译,而是上一次执行 编译结果被重用.

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.

因此可获得最佳性能:

  • 尝试(如果可能)避免在两次执行之间修改脚本.
  • 通过CSScript.CacheEnabled = true使用缓存(默认情况下启用)
  • 单个的调用的分派样式(Invoke(string methodName))可以 呼叫,但对于多个呼叫,请使用GetStaticMethod或更好的方法 AlignToInterface.性能优势非常重要,因为 与所有其他约定相比,调用依赖于 反射.这是Performance.cs示例的分析结果 脚本
  • 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

这篇关于有没有一种方法可以调用C#脚本文件,从而获得更好的性能结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆