[Tutor] Python vs. C

Alan Gauld alan.gauld at blueyonder.co.uk
Wed Jan 7 17:10:49 EST 2004


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

Thats a very good friend to have! :-)

> > wonder if I want to learn C too. Plus, I've gotten to like
programming
> > enough that I learning another language would be fun.

Thats a good enough reason to learn a new language.

> > just work on making my Python code more efficient or whatever?

Thats the best first step. Understanding how to identify
the bottleneckks and how to remove them in Python will
almost always be more useful than reprogramming a very
inefficient design in C.

> anyone knows the specific details of why C is faster

First thing to realize is that C is not always faster!
Someone already pointed out that sorting a list in C will
usually require you to write your own sort code, and Pythons
sort routine is almost certainly faster than anything you
will come up with as a beginner. And its in C...

Similarly using regiular expressions can be very difficult
and slow in C since it depemds on the library you use and
many standard C regex libraries are less efficient than the
Python re module.

So it depends on what you are doing whether C will even be
any faster.

> On the python vs C speed topic I want to bring in discussion, that's
the
> python interpreter has a looong startoff time


Actually it doesn't, but the initial compilation of the
python code can take a while. Compare my figures for your
Hello World tests:

> [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


$ time python hello.py
hello world

real    0m0.696s
user    0m0.040s
sys     0m0.140s



$ time python -c 'print "hello world"'
hello world

real    0m0.144s
user    0m0.060s
sys     0m0.090s



$ time bash hello.sh
hello world

real    0m0.079s
user    0m0.030s
sys     0m0.040s


$ time hello
Hello world

real    0m0.027s
user    0m0.020s
sys     0m0.030s

Notice the second one which evaluates the command from the
command line is much faster. It seems that its the loading
of the file and subsequent compilation that takes the bulk
of the time.(No doubt a guru can explain the discrepancy
better). And of course sh is still faster...

And notice that the pure C version at the end is not
significantly different to the sh version... and that
real time is less than sys time!! :-)

The lesson of all this is that performance is a complex
topic dependant on very many factors. If a program has
networking or database elements in it then they are likely
to dominate performance more than the code. In fact in my
work I can't remember having to tune code to fix
performance problems at any time in the last 5 years, its
always been fixed by tweaking the network or the database.

But to return to the original point, learning new languages
is a good thing because it will teach you more about
programming in general. And that will make you a better
programmer in any language.


Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld





More information about the Tutor mailing list