[Tutor] Python vs. C

Michael Janssen Janssen at rz.uni-frankfurt.de
Wed Jan 7 02:49:37 EST 2004

On Tue, 6 Jan 2004, Peter Jakubowicz wrote:

> Hi,
> I was talking with a friend who works as a programmer, and I mentioned that
> I was interested in learning C because it was faster than Python. And he
> asked me why it was faster; and I said because it's compiled and Python's
> interpreted; but he pressed me and I couldn't really say why C's being
> compiled makes it faster. And my friend left me to wonder about this: why
> is it faster? Intuitively, it makes sense to me that a language like
> assembly would be faster because I believe you can manipulate the chipset
> or whatever directly. Is that why C is faster, too? I am wondering about
> this because my Python programs are getting bigger, and occasionally I
> wonder if I want to learn C too. Plus, I've gotten to like programming
> enough that I learning another language would be fun. Or maybe I should
> just work on making my Python code more efficient or whatever? Anyway, if
> anyone knows the specific details of why C is faster or can point me to
> somewhere I can read about it I'd appreciate it. Thanks,

Hello Peter,

to begin with: learning yet another language is allways (well, often) a
good idea: even if the other language isn't faster, it will have it's
own strengths and own way of solving problems, which will help your mind
to view the programming world of another angle wich in turn will help to
abstract from "typicall language idioms" and see the problem in first
place. As Danny pointed out recently: do not apply this to "intercall"
or April Fool's languages like that ;-)

On the python vs C speed topic I want to bring in discussion, that's the
python interpreter has a looong startoff time (Yes, it's really bad: my
python "hello world" runs *twice* as long than a shell version (See
below for details). 10 times slower than GNU hello. Some might claim
that 40 ms in total arn't that long at all. But when your webserver shal
deliver a "hello world" multithousands times per minute it aches).
Remember: this is solely a matter of interpreter *startoff* time. That
means, running through a long programm doesn't do consume twice the
time, too. Not at all.

Another advantage of C is, that it gives you fine grained control about
memory consumption and datastructures: C hasn't got such strong buildin
"list" or "dict" but lets you define anything as strong and both best
suited for your needs. The flipside is, that you should implement it
good to gain increased performance. I can imaging that you would have a
very hard time competing with python list's sort method. list.sort is
(like many performance relevant parts) coded in C and for all I have
heard it's some of the best C code you can think of.

This lends to strategies for real-world performance enhancement: One
important rule is to do it only when needed. This means, first write
your programm good or nice or functional (perhaps all of these ;-) and
*then* look out for perfomance bottlenecks. Bottlenecks can be dissolved
by another algorithem (and writing python is fun enough to implement
some of them to choose the best) or - turn it over to C ("faine grained
control"). The good thing is, that python lets you embed C for only the
perfomance related parts.

My personal plan is to start learning C after I experienced severe
performance problems with an important one of my scripts. Furthermore my
plan is to identify only the bottleneck and learn how to embed enough C
to put it down. Then.


hello timings
[root at linus root]# echo 'print "hello world"' > hello.py
[root at linus root]# time python hello.py
hello world

real    0m0.042s
user    0m0.010s
sys     0m0.040s
[root at linus root]# echo 'echo "hello world"' > hello.sh
[root at linus root]# time sh hello.sh
hello world

real    0m0.018s
user    0m0.010s
sys     0m0.000s
[root at linus root]# apt-get install hello
[snip installation]

[root at linus root]# time hello
Hello, world!

real    0m0.004s
user    0m0.000s
sys     0m0.000s
[root at linus root]#

More information about the Tutor mailing list