Python中文
首页
教程
问答
标签
搜索
登录
注册
Windows环境下python库keychar的性能分析
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我运行此代码以查看来自google的<a href="http://www.keyczar.org/" rel="nofollow noreferrer">keyczar encryption library</a>对性能的影响:</p> <pre><code>from keyczar import keyczar, keys def main(iters): key = keys.RsaPrivateKey.Generate() msg = "ciao" crypt = None for i in range(iters): print i, "\r", crypt = key.Encrypt(msg) for i in range(iters): print i, "\r", key.Decrypt(crypt) if __name__ == '__main__': main(500) </code></pre> <p>在Windows下,500次迭代大约需要16分钟。在同一台机器上的ubuntu9.04分区下,500次迭代大约需要6秒。在</p> <p>我试过分析这个(cProfile+pstats),但我在解释结果方面没有太多经验。在</p> <p>有人能告诉我为什么同样的代码在Windows下运行速度慢150多倍吗?在</p> <hr/> <p>编辑2010-01-16</p> <p>这是我的一代_键.py脚本:</p> ^{pr2}$ <p>下面是在<code>generate_key</code>中创建统计文件的命令行:</p> <pre><code>C:\temp\python-keyczar-0.6b\tests\keyczar>python -m cProfile -o generate_key generate_key.py </code></pre> <p>下面是我的python会话以展开结果:</p> <pre><code>>>> import pstats >>> p = pstats.Stats('generate_key') >>> p.strip_dirs().sort_stats(-1).print_stats(25) Sat Jan 16 12:18:43 2010 generate_key 83493 function calls (82974 primitive calls) in 5.131 CPU seconds Ordered by: standard name List reduced from 564 to 25 due to restriction <25> ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.000 0.000 <string>:1(<module>) 1 0.000 0.000 0.210 0.210 AES.py:1(<module>) 1 0.022 0.022 0.210 0.210 AES.py:1(__bootstrap__) 1 0.000 0.000 0.000 0.000 DSA.py:115(DSAobj) 1 0.001 0.001 0.005 0.005 DSA.py:14(<module>) 1 0.000 0.000 0.000 0.000 DSA.py:174(DSAobj_c) 1 0.000 0.000 0.000 0.000 DSA.py:26(error) 1 0.000 0.000 0.001 0.001 RSA.py:125(size) 1 0.000 0.000 0.000 0.000 RSA.py:13(<module>) 1 0.000 0.000 0.000 0.000 RSA.py:140(publickey) 1 0.000 0.000 0.000 0.000 RSA.py:146(RSAobj_c) 1 0.000 0.000 0.000 0.000 RSA.py:23(error) 1 0.000 0.000 4.816 4.816 RSA.py:26(generate) 1 0.000 0.000 0.000 0.000 RSA.py:63(construct) 1 0.000 0.000 0.000 0.000 RSA.py:85(RSAobj) 1 0.003 0.003 0.004 0.004 SHA.py:4(<module>) 1 0.000 0.000 0.000 0.000 __future__.py:48(<module>) 1 0.000 0.000 0.000 0.000 __future__.py:74(_Feature) 7 0.000 0.000 0.000 0.000 __future__.py:75(__init__) 8 0.000 0.000 0.000 0.000 __init__.py:1(<module>) 1 0.000 0.000 0.000 0.000 __init__.py:11(<module>) 1 0.000 0.000 0.000 0.000 __init__.py:13(<module>) 1 0.000 0.000 0.000 0.000 __init__.py:18(<module>) 1 0.000 0.000 0.000 0.000 __init__.py:20(<module>) 2 0.000 0.000 0.000 0.000 __init__.py:24(<module>) <pstats.Stats instance at 0x023F5E40> >>> </code></pre> <p>所以Windows代码确实是在python中执行的。大部分运行时间都花在这里:</p> <pre><code>def generate(bits, randfunc, progress_func=None): """generate(bits:int, randfunc:callable, progress_func:callable) Generate an RSA key of length 'bits', using 'randfunc' to get random data and 'progress_func', if present, to display the progress of the key generation. """ obj=RSAobj() # Generate the prime factors of n if progress_func: progress_func('p,q\n') p = q = 1L while number.size(p*q) < bits: p = pubkey.getPrime(bits/2, randfunc) q = pubkey.getPrime(bits/2, randfunc) # p shall be smaller than q (for calc of u) if p > q: (p, q)=(q, p) obj.p = p obj.q = q if progress_func: progress_func('u\n') obj.u = pubkey.inverse(obj.p, obj.q) obj.n = obj.p*obj.q obj.e = 65537L if progress_func: progress_func('d\n') obj.d=pubkey.inverse(obj.e, (obj.p-1)*(obj.q-1)) assert bits <= 1+obj.size(), "Generated key is too small" return obj </code></pre> <p>我正在从<a href="http://www.voidspace.org.uk/downloads/pycrypto-2.1.0.win32-py2.6.zip" rel="nofollow noreferrer">here</a>运行PyCrypto downloade。在</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>PyCrypto有一个名为_fastmath的C模块,它使用gnump进行公钥操作。如果它不可用,它将使用Python的本机长整数,这要慢得多。在</p> <p>这两个文件是src/\u fastmath.c和lib/Crypto/PublicKey/_慢路径.py在</p> <p>Windows上的Python很可能不包括gnump,因此在Windows上它使用_慢路径.py在</p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
VirtualEnvRapper错误:路径python2(来自python=python2)不存在
6 回答
virtualenvs上的pyinstaller,没有名为导入错误的模块
5 回答
virtualenvs是否可以退回到用户包而不是系统包?
3 回答
virtualenvwrapper CentOS7
2 回答
virtualenvwrapper IOError:[Errno 13]权限被拒绝
1 回答
virtualenvwrapper mkproject和shell在windows中的启动问题?
2 回答
virtualenvwrapper mkvirtualenv不工作但没有错误
7 回答
Virtualenvwrapper python bash
9 回答
virtualenvwrapper:“workon”何时更改到项目目录?
10 回答
virtualenvwrapper:mkvirtualenv可以工作,但是rmvirtualenv返回bash:没有这样的文件或目录
3 回答
virtualenvwrapper:virtualenv信息存储在哪里?
9 回答
virtualenvwrapper:命令“python设置.pyegg_info“失败,错误代码为1
2 回答
virtualenvwrapper:如何将mkvirtualenv的默认Python版本/路径更改为ins
5 回答
Virtualenvwrapper:模块“pkg_resources”没有属性“iter_entry_points”
3 回答
Virtualenvwrapper:没有名为virtualenvwrapp的模块
4 回答
Virtualenvwrapper.bash_profi的正确设置
10 回答
Virtualenvwrapper.hook:权限被拒绝
1 回答
virtualenvwrapper.sh:fork:资源暂时不可用Python/Djang
5 回答
Virtualenvwrapper.shlssitepackages命令不工作
1 回答
Virtualenvwrapper.sh函数在bash sh中不可用
3 回答