Python:为什么 IDLE 这么慢? [英] Python: Why is IDLE so slow?

查看:73
本文介绍了Python:为什么 IDLE 这么慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

IDLE 是我最喜欢的 Python 编辑器.它提供了非常漂亮和直观的 Python shell,这对于单元测试和调试非常有用,并且是一个整洁的调试器.

然而,在 IDLE 下执行的代码非常慢.疯狂我的意思是3个数量级慢:

重击

time echo "for i in range(10000): print 'x',"|Python

需要 0.052 秒,

空闲

导入日期时间开始=日期时间.日期时间.现在()对于范围内的 i(10000):打印x",结束=日期时间.日期时间.现在()打印结束开始

需要:

<预><代码>>>>0:01:44.853951

大约慢 2,000 倍.

任何想法或想法如何改进?我想这与后台的调试器有关,但我不确定.

亚当

解决方案

问题是文本输出而不是调试器.

我刚刚在我的 Q6600(3GHz 超频)系统上尝试过,结果更糟.但是很容易看出,添加的输出文本越多,它们就会下降.

我试图用

运行它

1000 次迭代 => 7,8 秒2000 次迭代 => 28,5 秒3000 次迭代 => 70 秒

我过去做过一些低级 TK 的事情,我知道 TkText Widget 将文本保存在 BTree 结构中.一次附加一个字符是最糟糕的方法之一,但这似乎是 IDLE 正在做的事情.通常的方法是捕获更多数据并附加更大的文本块.

令人惊讶的是,如果您编写 print 'x\n' 输出速度要快得多.7 秒内迭代 3000 次,19 秒内迭代 10000 次.

所以问题肯定在于将单个字符附加到现有行.IDLE 程序员不知道 TkText 是如何工作的.

因此,建议在文本中添加更多换行符或输出更大的块,而不仅仅是单个x"字符.

IDLE is my favorite Python editor. It offers very nice and intuitive Python shell which is extremely useful for unit-testing and debugging, and a neat debugger.

However, code executed under IDLE is insanely slow. By insanely I mean 3 orders of magnitude slow:

bash

time echo "for i in range(10000): print 'x'," | python

Takes 0.052s,

IDLE

import datetime
start=datetime.datetime.now()
for i in range(10000): print 'x',
end=datetime.datetime.now()
print end-start

Takes:

>>> 0:01:44.853951

Which is roughly 2,000 times slower.

Any thoughts, or ideas how to improve this? I guess it has something to do with the debugger in the background, but I'm not really sure.

Adam

解决方案

The problem is the text output not the debugger.

I just tried it on my Q6600 (3GHz overclocked) System and my numbers are even worse. But its easy to see that they are going down the more output text is added.

I tried to run it with

1000 iterations => 7,8 sec 2000 iterations => 28,5 sec 3000 iterations => 70 sec

I did some low level TK stuff in the past and i know that the TkText Widget is keeping the text in a BTree structure. Appending text a character a time is one of the worst ways to do but this seems to be what IDLE is doing. The normal way is to catch more data and append a larger chunk of text.

Amazingly if you write print 'x\n' the output is much faster. 3000 iterations in 7 seconds and your 10000 in 19 sec.

So the problem is definitely with appending single chars to existing lines. The IDLE programmer didn't know how TkText works.

So the advise is to add more newlines into your text or output larger chunks and not only a single 'x' character.

这篇关于Python:为什么 IDLE 这么慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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