how fast is object-oriented Python code?

Peter Otten __peter__ at web.de
Thu Mar 4 11:36:41 EST 2004


beliavsky at aol.com wrote:

> I know that object-oriented numerical code in C++ can be much slower
> than the procedural C or Fortran equivalent, unless one is careful. To
> what extent is this true with OO and non-OO Python?

$ timeit.py -s"def x(): pass" "x()"
1000000 loops, best of 3: 0.437 usec per loop
$ timeit.py -s"class X:" -s"    def x(self): pass" -s"x=X()" "x.x()"
1000000 loops, best of 3: 0.694 usec per loop

The above bogus example would suggest an extra cost of 60 percent. 
Let's modify it a little:

$ timeit.py -s"class X:" -s"    def x(self): pass" -s"x=X().x" "x()"
1000000 loops, best of 3: 0.446 usec per loop

With a tiny change almost all of the overhead goes away. I think with a
grain of salt the same is true on a larger scale. The speed of an
application is determined by the quality of its design, not by the
programming paradigm employed.

Peter




More information about the Python-list mailing list