Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
768 views
in Technique[技术] by (71.8m points)

performance - Python: Why is IDLE so slow?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 ' 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...