C# application works much slower using compiled Ir

2019-05-26 06:41发布

问题:

When I load modules from standard library as source .py files in this application, elapsed time after end equals approximately 4 seconds:

Stopwatch watch = Stopwatch.StartNew();
ScriptEngine engine = Python.CreateEngine();
ScriptScope scope = engine.CreateScope();
ICollection<string> paths = engine.GetSearchPaths();
paths.Add(@"C:\Users\Montgomery1944\Documents\Libs\IronPython-2.7.1\Lib");
engine.SetSearchPaths(paths);
ScriptSource source = engine.CreateScriptSourceFromString(
@"from distutils import dir_util
import ftplib
import os
import tempfile
import zipfile");
source.Execute(scope);
watch.Stop();
Console.WriteLine(watch.Elapsed.Seconds);
Console.ReadLine();

But when I load modules from standard library compiled using script:

import System
import pyc
def get_files(dir):
    res = [file for file in System.IO.Directory.GetFiles(dir, '*.py')]
    dirs = System.IO.Directory.GetDirectories(dir)
    if dirs:
        for dir in dirs:
            res += get_files(dir)
    return res
files = get_files(r'C:\Program Files (x86)\IronPython 2.7.1\Lib')
pyc.Main(files + ['/out:IronPython.Stdlib'])

in this application, elapsed time after end equals approximately 20 seconds:

Stopwatch watch = Stopwatch.StartNew();
ScriptEngine engine = Python.CreateEngine();
ScriptScope scope = engine.CreateScope();
ScriptSource source = engine.CreateScriptSourceFromString(
@"import clr
clr.AddReference('IronPython.Stdlib')
from distutils import dir_util
import ftplib
import os
import tempfile
import zipfile");
source.Execute(scope);
watch.Stop();
Console.WriteLine(watch.Elapsed.Seconds);
Console.ReadLine();

Why loading modules from precompiled library is much slower than loading from source files?