启用调试时外部模块的 IronPython 性能较差
在尝试导入和使用 SymPy 时,我在 VS2010 内部的 IronPython 中遇到了一些性能问题。
我有一个包含以下代码的 IronPython 测试项目:
import time
print 'StandaloneIronPython'
startTime = time.time()
from sympy import *
importTime = time.time() - startTime
print 'Import time = %f' % importTime
startTime = time.time()
for x in (x/10.0 for x in xrange(-100,100)):
(x**2-2*x)
numericsTime = time.time() - startTime
print 'Basic numerics time= %f' % numericsTime
startTime = time.time()
for x in (x/10.0 for x in xrange(-100,100)):
N(x**2-2*x)
sympyTime = time.time() - startTime
print 'SymPy time = %f' % sympyTime
raw_input('Press enter to continue...')
SymPy 已作为 Egg 下载并安装;我的“IronPython 2.7\Lib\site-packages”文件夹位于项目搜索路径中。
如果我通过“调试 > 开始调试”运行程序,我会得到大约这样的结果:
StandaloneIronPython
Import time = 12.090019
Basic numerics time= 0.015594
SymPy time = 2.230804
Press enter to continue...
但是,如果我通过“调试 > 开始而不调试”运行程序,我会得到大约:
StandaloneIronPython
Import time = 2.199600
Basic numerics time= 0.015602
SymPy time = 0.140404
Press enter to continue...
我得到大约 5 倍的导入速度和 > 5 倍的速度。运行 sympy 速度的 10 倍。
那么,如果 IronPython 将所有时间都花在调试库代码上,我怎样才能从 IronPython 中获得良好的性能呢?
- 有没有一种方法可以将 SymPy 捆绑为库、程序集或其他东西,以便它可以快速执行?
- 有没有办法告诉VS2010不要调试SymPy的代码?
- 还有其他解决方案吗?
I am running into some performance problems with IronPython inside of VS2010 while trying to import and use SymPy.
I have an IronPython test project containing the following code:
import time
print 'StandaloneIronPython'
startTime = time.time()
from sympy import *
importTime = time.time() - startTime
print 'Import time = %f' % importTime
startTime = time.time()
for x in (x/10.0 for x in xrange(-100,100)):
(x**2-2*x)
numericsTime = time.time() - startTime
print 'Basic numerics time= %f' % numericsTime
startTime = time.time()
for x in (x/10.0 for x in xrange(-100,100)):
N(x**2-2*x)
sympyTime = time.time() - startTime
print 'SymPy time = %f' % sympyTime
raw_input('Press enter to continue...')
SymPy was downloaded and installed as an egg; my "IronPython 2.7\Lib\site-packages" folder is in the project Search Path.
If I run the program via "Debug > Start Debugging" I get approximately this:
StandaloneIronPython
Import time = 12.090019
Basic numerics time= 0.015594
SymPy time = 2.230804
Press enter to continue...
If, however, I run the program via "Debug > Start Without Debugging" I get approximately:
StandaloneIronPython
Import time = 2.199600
Basic numerics time= 0.015602
SymPy time = 0.140404
Press enter to continue...
I get ~5 times the speed importing and >10 times the speed running sympy.
So how can I get good performance out of IronPython if it's spending all its time debugging library code?
- Is there a way that I can bundle up SymPy as a library, assembly, or something else such that it will perform quickly?
- Is there a way to tell VS2010 not to debug the code for SymPy?
- Is there some other sort of solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每当您看到附加调试器和未附加调试器的运行之间存在巨大性能差异时,通常是因为您的应用程序生成了大量调试器必须处理的事件。
最常见的例子是异常。如果 SymPy 在初始化期间抛出并捕获大量异常,则每个异常都会导致调试器往返(即使已处理),从而导致速度大幅下降。
您可以通过转到“调试 > 异常...”、选中“公共语言运行时异常”旁边的“抛出”框并重新运行应用程序来验证这一点。如果你遇到数百个异常就停下来,那就是你的问题了。
如果这确实是您的问题,那么不幸的是没有一个好的解决方法。将 Python 代码预编译到 .dll 中并不能阻止抛出这些异常。
Whenever you see a big performance discrepancy between running with and without a debugger attached, it's usually because your app is generating a ton of events that the debugger has to handle.
The most common example of this is exceptions. If SymPy is throwing and catching lots and lots of exceptions during initialization, each exception causes a round trip to the debugger (even if it's handled), causing a massive slowdown.
You can verify this by going to "Debug > Exceptions...", checking the "Thrown" box next to "Common Language Runtime Exceptions" and re-running the app. If you stop at hundreds of exceptions, there's your problem.
If that is indeed your problem, unfortunately there's not a good workaround for it. Pre-compiling your Python code into a .dll won't prevent those exceptions from being thrown.