为什么IronPython在构建为exe然后作为脚本构建时导入模块的速度要慢得多?

时间:2022-11-15 00:58:10

I wrote a small script for work and when developing it everything was fine but when I turned it over for production use people complained about the time it took to display the first form. Looking into it, all the time seemed to be spent in importing various modules used by the script. I made a new script of just the import statements and a print Hello World and then ran it both as a python script and an executable with the results below. What's going on and is there any way to speed this up?

我写了一个小脚本用于工作,当开发它时一切都很好但是当我把它翻过来用于生产时,人们抱怨显示第一个表单所花费的时间。在调查中,似乎总是花费在导入脚本使用的各种模块上。我创建了一个只包含import语句和打印Hello World的新脚本,然后将它作为python脚本和可执行文件运行,结果如下。发生了什么事,有什么方法可以加快速度吗?

Python file:

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

exe built with ipy pyc.py /main:time.py /target:exe

使用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

contents of time.py:

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 个解决方案

#1


0  

A combination of adding /platform:x86 to the pcy.py call and running ngen on the resulting dll and exe reduced the execution time of the exe to 1/2 that of the invoking it as a script. I'd call that successful.

将/ platform:x86添加到pcy.py调用并在生成的dll和exe上运行ngen的组合将exe的执行时间减少到调用它作为脚本的1/2。我称之为成功。

#1


0  

A combination of adding /platform:x86 to the pcy.py call and running ngen on the resulting dll and exe reduced the execution time of the exe to 1/2 that of the invoking it as a script. I'd call that successful.

将/ platform:x86添加到pcy.py调用并在生成的dll和exe上运行ngen的组合将exe的执行时间减少到调用它作为脚本的1/2。我称之为成功。