24

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

但是,在 IDLE 下执行的代码非常慢。疯狂地我的意思是慢了 3 个数量级:

重击

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

耗时 0.052 秒,

闲置的

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

需要:

>>> 0:01:44.853951

这大约慢了 2,000 倍。

任何想法或想法如何改善这一点?我想这与后台的调试器有关,但我不太确定。

亚当

4

2 回答 2

31

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.

于 2010-02-06T10:33:07.653 回答
10

问题在于 Tkinter Text 小部件,它对非常长的行的管理效率低下,而您创建了一个。你会注意到,虽然很长的一行的任何部分都是可见的,但所有滚动都非常缓慢。

于 2010-02-06T13:59:13.227 回答