加快Python命令行应用程序的初始执行速度 [英] Speed Up Initial Execution of Python Command Line App

查看:151
本文介绍了加快Python命令行应用程序的初始执行速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为使用Python和 argparse 。现在,它基本上只是启动Vim,然后将缓冲区推入SQLite数据库。

I've prototyped a command line application for quick note-taking using Python and argparse. Right now it basically just launches Vim and then shoves the buffer into a SQLite database.

问题是,在这两个平台上加载Python的速度都我的机器(〜2 / 3GHz Intel Core 2 Duos)和最基本的功能(在启动时打印帮助菜单)可能需要一秒钟。我知道我的代码很好,因为Python速度非常快,并且一旦加载Python,交互模式就很敏捷,但是我可以用一个简单的Hello Word来模仿我的烦恼:

The problem is, loading Python is slow on both of my machines (~2/3GHz Intel Core 2 Duos) and the most basic functionality (printing a help menu on startup) can take over a second. I know my code is fine because Python is perfectly fast and the interactive mode is snappy once Python loads, but I can mimic my annoyance with a simple Hello Word:

$ time python -c "print 'hello world'"
hello world
real    0m0.669s
user    0m0.070s
sys     0m0.041s

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

Of course, the problem is not unique to Python:

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

我的问题是:如何加快Python应用程序的初始执行速度?我希望我的程序看起来像 git wc

My question is: How can I speed up the initial execution of a Python application? I want my program to feel like git or wc.

系统:我在OS X 10.6上遇到python2.6的问题.8以及在OS X 10.7.2上使用python2.7。

System: I encounter this problem with python2.6 on OS X 10.6.8 and with python2.7 on OS X 10.7.2.

注意:随后执行 python (和 erl )的速度要快得多,但是我已经在对该程序进行审查,并且希望它真正变得敏捷。

Note: Subsequent executions of python (and erl) are much faster, but I'm already dogfooding this program and I want it to be truly snappy.

更新:我尝试运行 pypy ,发现它的初始加载时间与python2类似我的两个系统上分别为.6和2.7(初始加载时为〜.5秒),与OS X 10.6.8上的python2.6相比,后续调用的性能为一半(pypy为〜.08s,2.6为〜.35s),与OS X 1上的python2.7相比,后续调用的性能类似0.7.2(对于pypy python2.7为〜.08s)。

Update: I've tried running pypy and find it has similar initial load time to python2.6 and 2.7 on both of my systems (~.5 seconds on initial load), half the performance on subsequent calls compared to python2.6 on OS X 10.6.8 (~.08s for pypy, ~.35s for 2.6), and similar performance on subsequent calls compared to python2.7 on OS X 10.7.2 (~.08s for pypy and python2.7).

更新2: dr的输出jimbob的建议(这似乎使初始加载时间缩短了2/3)-

Update 2: Output from dr jimbob's suggestion (this seems to trim the initial load time by 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


推荐答案

利用类似的te臭名昭著的 Microsoft Office快速入门或 Java快速入门以及IIRC甚至 Adob​​e Fast-something所使用的技术……

You can make use of a similar technique used by the infamous "Microsoft Office Quick Start" or the "Java Quick Start" and, IIRC even the "Adobe Fast-something"...

尝试将程序的所有库始终保持在磁盘缓存中。

The trick is to try and keep all the libraries of the program in the disk cache, all the time.

您可以使用简单的crontab命令来获取它,该命令被编程为每次运行一次小时。确切的详细信息将取决于您的系统,但是应该与类似的东西一起工作:

You can get it with a simple crontab command, programmed to be run once each hour. The exact details will depend on your system, but should work with something like:

$ crontab -e
0 * * * * python -c 'pass'

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

Although to me more effective you should write a simple Python script that imports all the modules your program is using and then just ends.

这篇关于加快Python命令行应用程序的初始执行速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆