writing python extensions in assembly

D'Arcy J.M. Cain darcy at druid.net
Fri May 16 11:42:57 EDT 2008


On Fri, 16 May 2008 11:21:39 -0400
"inhahe" <inhahe at gmail.com> wrote:
> You could be right, but here are my reasons.
> 
> I need to make something that's very CPU-intensive and as fast as possible. 
> The faster, the better, and if it's not fast enough it won't even work.
> 
> They say that the C++ optimizer can usually optimize better than a person 
> coding in assembler by hand can, but I just can't believe that, at least for 
> me, because when I code in assembler, I feel like I can see the best way to 
> do it and I just can't imagine AI would even be smart enough to do it that 
> way...

Perhaps.  Conventional wisdom says that you shouldn't optimize until
you need to though.  That's one of the benefits of the way Python
works.  Here's how I would do it.

1. Write the code (call it a prototype) in pure Python.  Make sure that
everything is modularized based on functionality.  Try to get it split
into nice, bite size chunks.  Make sure that you have unit tests for
everything that you write.

2. Once the code is functioning, benchmark it and find the
bottlenecks.  Replace the problem methods with a C extension.  Refactor
(and check your unit tests again) if needed to break out the problem
areas into as small a piece as possible.

3.  If it is still slow, embed some assembler where it is slowing down.

One advantage of this is that you always know if your optimizations are
useful.  You may be surprised to find that you hardly ever need to go
beyond step 1 leaving you with the most portable and easily maintained
code that you can have.

> For portability, I'd simply write different asm routines for different 
> systems.  How wide a variety of systems I'd support I don't know.  As a bare 
> minimum, 32-bit x86, 64-bit x86, and one or more of their available forms of 
> SIMD.

Even on the same processor you may have different assemblers depending
on the OS.

-- 
D'Arcy J.M. Cain <darcy at druid.net>         |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on
+1 416 425 1212     (DoD#0082)    (eNTP)   |  what's for dinner.



More information about the Python-list mailing list