为什么将IronPython编译为exe文件后导入模块速度比作为脚本慢得多?

5

我为工作编写了一个小脚本,在开发过程中一切顺利,但当我交付生产使用时,人们抱怨显示第一个表单所需的时间很长。经过调查,所有时间似乎都花在导入脚本使用的各种模块上。我制作了一个新的脚本,只包含导入语句和打印Hello World,并分别作为Python脚本和可执行文件运行,结果如下。怎么回事,有没有办法加快速度?

Python文件:

$ for i in {1..10}
> do
>   time ./ipy.exe time.py
> done
real    0m1.712s
real    0m1.701s
real    0m1.689s
real    0m1.691s
real    0m1.709s
real    0m1.663s
real    0m1.697s
real    0m1.700s
real    0m1.699s
real    0m1.741s

使用ipy pyc.py /main:time.py /target:exe编译的exe文件

$ for i in {1..10}
> do
>   time ./time.exe | grep -v user | grep -v sys
> done
real    0m22.119s
real    0m22.116s
real    0m22.133s
real    0m21.816s
real    0m21.985s
real    0m21.785s
real    0m22.010s
real    0m21.686s
real    0m21.877s
real    0m21.944s

time.py的内容:

import clr
from clr import AddReference
AddReference("System.Windows.Forms")
AddReference("System.Drawing")
AddReference("p4api")
import cgi
from System.Diagnostics import Process
from P4API import *
import System
from System import *
from System.Windows.Forms import *
from System.ComponentModel import *
from System.Drawing import *
from System.Threading import *
import re
import urllib
import os
import tokenize
from cStringIO import StringIO
from optparse import OptionParser
import os
import urllib
import ntpath
import stat
import genericpath
import warnings
import linecache
import types
import UserDict
import _abcoll
import abc
import textwrap
import string
import urlparse
import collections
import keyword
import nturl2path
import mimetools
import tempfile
import random
import __future__
import rfc822
import tokenize
import token
import codecs
import ConfigParser
import uuid
import sys

print "Hello World"

1
将/platform:x86添加到构建脚本中,将可执行文件的启动时间从22秒减少到4秒,但仍然是.py脚本的250%。 - Doug-W
1个回答

1

在 pcy.py 调用中添加 /platform:x86 并对生成的 dll 和 exe 运行 ngen,将 exe 的执行时间减少到作为脚本调用的一半。我认为这是成功的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接