Is python very slow compared to C

R

rtilley

I have just started to learn python. Some said that its slow. Can
somebody pin point the issue.

It depends on what you are doing. Much of Python is just wrapped C. So
many things are very fast.
 
D

diffuser78

I have just started to learn python. Some said that its slow. Can
somebody pin point the issue.

Thans
 
R

Robert Hicks

Most languages are slow compared to "C". Python is fast enough for just
about anything you want to do with it.

Robert
 
B

bearophileHUGS

Yes this language is very slow, but frequently this isn't a problem,
because you are using fast functions/libraries written in C, or you are
doing I/O bound operations. And Psyco, numeric/numarray, ShedSkin can
help in some other cases (Pyrex and other solutions allow you to mix in
lower level languages). If you are looking for pure algorithmic speed
you probably have to go look for other languages, like C, C++, Ocaml,
typed Lisp code, etc.

Bye,
bearophile
 
L

Larry Bates

I have just started to learn python. Some said that its slow. Can
somebody pin point the issue.

Thans
"Some" doesn't know what he/she/they are talking about.
Generalizations like that upset me because it shows someone
that has some predisposition to some other language and they
don't want to be confused by "facts". Many "compiled
language" programmers fall into this category. Many times
you can write, debug, execute, and document a Python program
before "compiled language" guys can even get started. Many
believe that Python is also more maintainable over time.
When you come back to it in 6 months you will ACTUALLY BE ABLE
TO READ THE CODE. Productivity cannot be measured by
benchmarks alone.

Give Python a try and I'll bet you find that it is fast enough
(that's all that is important right) for all but device
drivers and highly scientific applications (and even then
there are solutions). The parts of Python that need to be
fast have been rewritten as binary libraries. That way you
get the best of both worlds: high level language that has
fast libraries when required.

-Larry Bates
 
M

Michael Tobis

Experienced programmers learning Python should read this:

http://effbot.org/zone/python-objects.htm

Try to understand the advantages of this approach, and understand its
costs.

Essentially, everything you do in Python (or related languages)
involves resolving a name. Sometimes the number of dictionary look-ups
(name dereferences) required to do anything is very expensive in
runtime performance.

If your code is not compute-bound (as most codes are not; these days
communication-bound or limited by database performance are more common)
the increase in your own performance will be much more important. If
your code does do so much computing (as mine do) that performance
matters, you may still find Python useful, but it is less of a
slam-dunk.

mt
 
S

Steven D'Aprano

I have just started to learn python. Some said that its slow.

No, you can learn the basics of Python is only a few hours, and become
very proficient at it in days or weeks. It is much faster to learn Python
than to learn C.
Can somebody pin point the issue.

What is slow? Slow compared to what? Would you prefer to spend three days
writing a program that will run in twenty milliseconds, or three hours
writing the same program which runs in eighty milliseconds? Is the
computer's execution time more important than your development time? If
the computer's time is more valuable than your time, then you should be
writing in assembly language.

Python helps you write shorter code with fewer bugs, much quicker, than C.
If you discover a specific problem that runs too slow in Python, it is
possible to write a C extension to solve that specific problem, while
still having all the other advantages of Python.
 
S

Steven D'Aprano

Yes this language is very slow,

Very slow to do what, compared to what? The decay time of the tau meson?

Slowness is not an absolute quantity. Slowness is relative, and comments
like "Python is very slow" just reinforces the meme that execution speed
is the only important factor -- even if you follow up with a good (and
correct) description of why raw CPU speed is rarely an issue, it is too
late, you've reinforced the meme "only C is good".

I would challenge the assertion that *any* serious modern language is
"very slow" on modern hardware in any meaningful sense. There are plenty
of *specific tasks* which are too slow when written in one language or
another, but that's not the same thing.
 
B

bearophileHUGS

Steven D'Aprano>Very slow to do what, compared to what? The decay time
of the tau meson?<

Probably every answer I can give you is wrong for you, so answering is
almost useless... In this thread we have already given the most
pertinent answers to the original question from Diffuse.
I can show you this page, but I think this is useless too for you:
http://shootout.alioth.debian.org/gp4/benchmark.php?test=all&lang=python&lang2=ocaml

And, by the way, this is false:
Robert Hicks>Python is fast enough for just about anything you want to
do with it.<

If you are on a Unix-like OS, then the page about Weave from SciPy can
show you that sometimes Python isn't quick enough.

Bye,
bearophile
 
B

bonono

Steven D'Aprano>Very slow to do what, compared to what? The decay time
of the tau meson?<

Probably every answer I can give you is wrong for you, so answering is
almost useless... In this thread we have already given the most
pertinent answers to the original question from Diffuse.
I can show you this page, but I think this is useless too for you:
http://shootout.alioth.debian.org/gp4/benchmark.php?test=all&lang=python&lang2=ocaml

This is even more interesting:

http://shootout.alioth.debian.org/gp4/benchmark.php?test=all&lang=python&lang2=lua

However, to me, the strength of python is the batteries that is
included(and there are more and more coming).
 
F

Felipe Almeida Lessa

Em Dom, 2006-02-12 às 03:03 -0800, (e-mail address removed) escreveu:
Probably every answer I can give you is wrong for you, so answering is
almost useless... In this thread we have already given the most
pertinent answers to the original question from Diffuse.
I can show you this page, but I think this is useless too for you:
http://shootout.alioth.debian.org/gp4/benchmark.php?test=all〈=python&lang2=ocaml

What they were saying is that at most of the times your program is going
to sit and wait for things like network, hard drive, user input, etc.,
and these cannot be changed by any language. Those benchmarks test only
raw CPU performance.

Yes, sometimes you *need* raw CPU power, but nobody said that in this
case Python is good for you. Python is good at making your life as a
programmer easier, and every Python programmer knows that.

I wanted to see PyRex or C modules versions of that benchmarks, it would
be really nice. This is the approach used to create fast CPU-bound
algorithms in Python, and as such should be tested as thoroughly as
those Python-only counterparts.

And if it matters for you: Google says Python is fast for them, what
else do you want?

Cya,
Felipe.

--
"Quem excele em empregar a força militar subjulga os exércitos dos
outros povos sem travar batalha, toma cidades fortificadas dos outros
povos sem as atacar e destrói os estados dos outros povos sem lutas
prolongadas. Deve lutar sob o Céu com o propósito primordial da
'preservação'. Desse modo suas armas não se embotarão, e os ganhos
poderão ser preservados. Essa é a estratégia para planejar ofensivas."

-- Sun Tzu, em "A arte da guerra"
 
F

Felipe Almeida Lessa

Em Dom, 2006-02-12 às 03:20 -0800, (e-mail address removed) escreveu:
However, to me, the strength of python is the batteries that is
included(and there are more and more coming).

So .NET is as good as Python? Hmmm... I think the language itself is the
best part of Python, its library is just a complement (a very good and
relevant one, though).

--
"Quem excele em empregar a força militar subjulga os exércitos dos
outros povos sem travar batalha, toma cidades fortificadas dos outros
povos sem as atacar e destrói os estados dos outros povos sem lutas
prolongadas. Deve lutar sob o Céu com o propósito primordial da
'preservação'. Desse modo suas armas não se embotarão, e os ganhos
poderão ser preservados. Essa é a estratégia para planejar ofensivas."

-- Sun Tzu, em "A arte da guerra"
 
J

Jarek Zgoda

(e-mail address removed) napisa³(a):
I have just started to learn python. Some said that its slow. Can
somebody pin point the issue.

So what? If you want fast code, go with assembly.
 
B

bonono

Felipe said:
Em Dom, 2006-02-12 às 03:20 -0800, (e-mail address removed) escreveu:

So .NET is as good as Python? Hmmm... I think the language itself is the
best part of Python, its library is just a complement (a very good and
relevant one, though).
..NET is not a language, IMO.
 
F

Felipe Almeida Lessa

Em Dom, 2006-02-12 às 04:33 -0800, (e-mail address removed) escreveu:
.NET is not a language, IMO.

You talked about "batteries included", and that isn't related to the
language. I said .NET in reference to what you said. The part about the
language is my own opinion, I just didn't want to say anything about C#,
Boo, J#, Nemerle or any other language that targets the .NET framework.
In the case of Python, as well as Java, the language has the same name
as the framework, and this may have lead you to mistake me.

--
"Quem excele em empregar a força militar subjulga os exércitos dos
outros povos sem travar batalha, toma cidades fortificadas dos outros
povos sem as atacar e destrói os estados dos outros povos sem lutas
prolongadas. Deve lutar sob o Céu com o propósito primordial da
'preservação'. Desse modo suas armas não se embotarão, e os ganhos
poderão ser preservados. Essa é a estratégia para planejar ofensivas."

-- Sun Tzu, em "A arte da guerra"
 
?

=?iso-8859-1?q?Luis_M._Gonz=E1lez?=

Steven said:
What is slow? Slow compared to what? Would you prefer to spend three days
writing a program that will run in twenty milliseconds, or three hours
writing the same program which runs in eighty milliseconds? Is the
computer's execution time more important than your development time? If
the computer's time is more valuable than your time, then you should be
writing in assembly language.

Python helps you write shorter code with fewer bugs, much quicker, than C.
If you discover a specific problem that runs too slow in Python, it is
possible to write a C extension to solve that specific problem, while
still having all the other advantages of Python.

You are right, we all know that, but I think the person who asked this
question doesn't want to hear a sales pitch. He asked a very specific
question regarding execution speed.
 
G

Guest

Felipe Almeida Lessa said:
In the case of Python, as well as Java, the language has the same
name as the framework, and this may have lead you to mistake me.

Not really, in either case. There's Python for both .NET and for the
Java VM.
 
S

Steven D'Aprano

Steven D'Aprano>Very slow to do what, compared to what? The decay time
of the tau meson?<

Probably every answer I can give you is wrong for you, so answering is
almost useless...

We do actually agree. You did explain why the speed of the language itself
is rarely the critical factor. My criticism is that whatever good your
post would have done was nullified by your opening comment stating that
Python is very slow -- a comment which I think is not only harmful, but
wrong, benchmarks like the one you linked to not withstanding.

I think it is wrong to call Python "very slow" just because it is slower
than some other language or languages, for the same reason it would be
wrong to describe the population of the UK as "very low" because 60
million people is a smaller number than China or India's one billion plus.
Doing so merely reinforces the premature optimizer's message that any
language that isn't C (and sometimes Lisp) is "not fast enough".

The benchmark you pointed to are of limited use for application
developers. (Their value to language designers is another story.) Given
that Ocaml is (say) 200 times faster than Python on average, it tells the
application developer virtually nothing about the performance of his
application. And that's what user's care about -- they couldn't care less
about binary trees or partial sums, they care about how long it takes to
render a JPEG, open an email, save their files, query the database,
display a window, and so forth. Few application level tasks are limited by
the speed of the language, not these days.

You don't believe me? Consider the following:

When you drag your mouse over text in a graphical text editor, the text
highlights. How much money would you be prepared to pay to make that
highlighting occur 200 times faster? $100? $1? One cent? Chances are you
wouldn't pay a thing, because it is already fast enough, and making it
faster is of zero value to you -- even if highlighting ten gigabytes of
text might be uncomfortably slow.

What about opening an email? How much would you pay to reduce the time it
takes to open and display an email from a hundredth of a second to
virtually instantaneously? I suggest that most people would consider 0.01s
to already be be "virtually instantaneously".

The important question isn't "is Python fast or slow?", it is "is Python
fast enough?". That's a question that doesn't have a simple answer,
because it depends. Fast enough to do what?

But, in general, more often than not, Python is fast enough. The extra
value of using something like Lua or Ocaml or even C is just not enough to
make up for the disadvantages of using those languages.

Of course Python isn't always fast enough. Please don't waste your time
coming up with practical examples where pure Python is objectively too
slow for a certain task, because you won't be telling me anything I don't
already know.

But the developer doesn't have to write pure Python, he can call a module
written in C, or change the algorithm, or find another way to accomplish
the same end result. Or even decide that for this specific task, Python is
not the language to use. These are all good solutions, but they don't mean
that Python is "very slow" in general. Even C is not always fast enough,
but we wouldn't say C is "very slow" just because it can't calculate the
first million Mersenne Primes in twenty milliseconds.
 
S

Steven D'Aprano

You are right, we all know that, but I think the person who asked this
question doesn't want to hear a sales pitch. He asked a very specific
question regarding execution speed.

Read his post again. He didn't ask a specific question at all, and he
certainly didn't mention execution speed. He asked a vague, meaningless
question about whether Python was "slow compared to C".

Slow to learn? No, Python is easier to learn than C.

Slow to compile? No, Python handles compiling to byte-code transparently,
you will rarely even notice it.

Slow to debug? That depends on the nature of the bug, but generally no.
It is much easier to write correct code the first time in Python than in
C, and even if the code isn't correct, Python makes it easier (and
therefore faster) to debug it.

Slow to execute? No, for many common tasks execution speed is either fast
enough regardless of the language, or it is limited by things like user
input, I/O, memory or other factors independent of the language the code
is written in.

If the task is limited by the CPU and not I/O etc., pure Python code with
no C extensions or Psycho optimizations might be slower than C code, but
that tells us virtually nothing about whether it is too slow. After all,
my car is slower than a F-11 fighter plane, but my car is plenty fast
enough for driving to the shop and back, and a car is a far more practical
solution to the task even though a F-11 would be faster.
 
B

bonono

Steven said:
But, in general, more often than not, Python is fast enough. The extra
value of using something like Lua or Ocaml or even C is just not enough to
make up for the disadvantages of using those languages.
What is the disavantage of Lua comparing with Python ? Or Ocaml
comparing with Python ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top