在IronPython中编译代码时没有性能改进

本文关键字:性能 IronPython 编译 代码 | 更新日期: 2023-09-27 18:06:10

我有问题,在某些情况下,ScriptSourceExecute方法需要很长时间。这是有问题的,因为我经常对相同的ScriptSources使用这种方法。现在我试着编译ScriptSource并保存结果对象。然后我在编译的代码上使用Execute方法,但这似乎和ScriptSource上的Execute一样慢。

我做了一个简单的例子:

private static Dictionary<string, CompiledCode> compiledCode = new Dictionary<string, CompiledCode>();
// Method
public void LoadScript(string Name)
{
    if (compiledCode.ContainsKey(Name))
    {
        compiledCode[Name].Execute(scriptScope);
    }
    else
    {
        ScriptSource source = IronPythonScriptHost.Singleton.GetScriptSource(Name);
        CompiledCode code = source.Compile();
        compiledCode.Add(Name, code);
        code.Execute(scriptScope);     // Use an instanced ScriptScope                     
    }
}

为什么编译后的代码不更快?

还是我做错了什么?

谢谢

在IronPython中编译代码时没有性能改进

我找到问题了。在我的情况下,我覆盖了IronPython中的Import方法,我自己的实现真的很慢,所以导入部分是我的问题。我现在已经预编译了所有模块并缓存了它们,所以在import方法中访问它们非常快。就这些,谢谢!