Calling a C program from a Python Script

Steven Bethard steven.bethard at gmail.com
Thu Dec 9 19:54:08 CET 2004


It's me wrote:
> I would expect C to run circles around the same operation under Python.

You should probably only expect C to run circles around the same 
operations when those operations implemented entirely in Python.  In the 
specific (trivial) example given, I wouldn't expect Python to be much 
slower:

>>for root, files, dirs in os.walk(path)
>>      for f in files:
>>          try:
>>              x = file(f, 'rb')
>>              data = x.read()
>>              x.close()

Remember that CPython is implemented in C, and so all the builtin types 
(including file) basically execute C code directly.  My experience with 
Python file objects is that they are quite fast when you're doing simple 
things like the example above.  (In fact, I usually find that Python is 
faster than Java for things like this.)

Of course, the example above is almost certainly omitting some code that 
really gets executed, and without knowing what that code does, it would 
be difficult to predict exactly what performance gain you would get from 
reimplementing it in C.  Profile the app first, find out where the tight 
spots are, and then reimplement in C if necessary (often, it isn't).

STeve



More information about the Python-list mailing list