Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Why is IDLE so slow?

Tags:

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

like image 373
Adam Matan Avatar asked Feb 06 '10 09:02

Adam Matan


People also ask

Why is my Python program so slow?

In summary: code is slowed down by the compilation and interpretation that occurs during runtime. Compare this to a statically typed, compiled language which runs just the CPU instructions once compilated. It's actually possible to extend Python with compiled modules that are written in C.

Is IDLE good for Python?

Its major features include the Python shell window(interactive interpreter), auto-completion, syntax highlighting, smart indentation, and a basic integrated debugger. IDLE is a decent IDE for learning as it's lightweight and simple to use. However, it's not for optimum for larger projects.

How do I fix Python IDLE?

Type python and hit enter to turn windows command prompt to python idle (make sure python is installed). Type quit() and hit enter to turn it back to windows command prompt. Type cls and hit enter to clear the command prompt/ windows shell.

Is Python actually slow?

In a nutshell. So to sum it all up here python is slow mainly because of the two main reasons. One is dynamically types language which means, unlike in java, python has no variable declaration and this makes it quite long to compile and sometimes the variables get changed during the run without our knowledge.


2 Answers

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.

like image 111
Lothar Avatar answered Sep 21 '22 10:09

Lothar


The problem is in the Tkinter Text widget, and its inefficient management of very long lines, and you create one. You'll notice that, while any part of a very long line is visible, all scrolling is devilishly slow.

like image 23
tzot Avatar answered Sep 21 '22 10:09

tzot