当我从标准库中加载模块作为此应用程序中的源.py文件时,结束后经过的时间大约等于4秒:
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();
但是当我从使用脚本编译的标准库中加载模块时:
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'])
在本申请中,结束后经过的时间约等于20秒:
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();
为什么从预编译库加载模块比从源文件加载要慢得多?