加速Python命令行应用程序的初始执行

Cod*_*ess 9 python command-line

我已经使用Python和argparse对命令行应用程序进行了原型设计,以便快速记笔记.现在它基本上只是启动Vim然后将缓冲区推送到SQLite数据库.

问题是,加载Python是缓慢的两个我的机器(〜2/3GHz的英特尔酷睿2二重奏)和最基本的功能(打印在启动时帮助菜单)可以接管第二.我知道我的代码很好,因为Python非常快,并且一旦Python加载,交互模式就会很快,但我可以通过一个简单的Hello Word来模仿我的烦恼:

$ time python -c "print 'hello world'"
hello world
real    0m0.669s
user    0m0.070s
sys     0m0.041s
Run Code Online (Sandbox Code Playgroud)

当然,问题不是Python独有的:

$ time erl -noshell -eval 'io:fwrite("Hello, World!\n"), init:stop().'
Hello, World!
real    0m2.824s
user    0m0.253s
sys     0m0.104s
Run Code Online (Sandbox Code Playgroud)

我的问题是: 如何加快Python应用程序的初始执行速度? 我希望我的程序感觉像gitwc.

系统:我在OS X 10.6.8上使用python2.6,在OS X 10.7.2上使用python2.7遇到此问题.

注意:(pythonerl)的后续执行速度要快得多,但我已经在为这个程序做好准备了,我希望它真的很快.

更新:我已经尝试运行pypy并发现它在我的两个系统上都有类似的初始加载时间到python2.6和2.7(初始加载时约为0.5秒),后续调用的性能是OS上python2.6的一半X 10.6.8(pypy为〜.08s,2.6为〜.35s),后续调用的性能与OS X 10.7.2上的python2.7相比(pypy python2.7 为〜.08s ).

更新2:来自jimbob博士建议的输出(这似乎将初始加载时间减少了2/3) -

$ python -vSEc "print 'hello world'"
# installing zipimport hook
import zipimport # builtin
# installed zipimport hook
import encodings # directory /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/encodings
# /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/encodings/__init__.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/encodings/__init__.py
import encodings # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/encodings/__init__.pyc
# /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/codecs.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/codecs.py
import codecs # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/codecs.pyc
import _codecs # builtin
# /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/encodings/aliases.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/encodings/aliases.py
import encodings.aliases # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/encodings/aliases.pyc
# /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/encodings/utf_8.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/encodings/utf_8.py
import encodings.utf_8 # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/encodings/utf_8.pyc
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
hello world
# clear __builtin__._
# clear sys.path
# clear sys.argv
# clear sys.ps1
# clear sys.ps2
# clear sys.exitfunc
# clear sys.exc_type
# clear sys.exc_value
# clear sys.exc_traceback
# clear sys.last_type
# clear sys.last_value
# clear sys.last_traceback
# clear sys.path_hooks
# clear sys.path_importer_cache
# clear sys.meta_path
# clear sys.flags
# clear sys.float_info
# restore sys.stdin
# restore sys.stdout
# restore sys.stderr
# cleanup __main__
# cleanup[1] zipimport
# cleanup[1] _codecs
# cleanup[1] signal
# cleanup[1] encodings
# cleanup[1] encodings.utf_8
# cleanup[1] encodings.aliases
# cleanup[1] exceptions
# cleanup[1] _warnings
# cleanup[1] codecs
# cleanup sys
# cleanup __builtin__
# cleanup ints: 3 unfreed ints
# cleanup floats

real    0m0.267s
user    0m0.009s
sys     0m0.043s
Run Code Online (Sandbox Code Playgroud)

rod*_*igo 2

您可以利用臭名昭著的“Microsoft Office Quick Start”或“Java Quick Start”以及 IIRC 甚至“Adobe Fast-something”所使用的类似技术...

诀窍是尝试始终将程序的所有库保留在磁盘缓存中。

您可以使用简单的 crontab 命令来获取它,该命令被编程为每小时运行一次。确切的细节取决于您的系统,但应该适用于以下内容:

$ crontab -e
0 * * * * python -c 'pass'
Run Code Online (Sandbox Code Playgroud)

虽然对我来说更有效,但你应该编写一个简单的 Python 脚本来导入你的程序正在使用的所有模块,然后结束。