I need to speed up a function. Should I use cython, ctypes, or something else?

Posted by Peter Stewart on Stack Overflow See other posts from Stack Overflow or by Peter Stewart
Published on 2010-04-15T16:23:08Z Indexed on 2010/04/15 16:33 UTC
Read the original article Hit count: 256

Filed under:
|
|
|

I'm having a lot of fun learning Python by writing a genetic programming type of application.

I've had some great advice from Torsten Marek, Paul Hankin and Alex Martelli on this site.

The program has 4 main functions:

  • generate (randomly) an expression tree.
  • evaluate the fitness of the tree
  • crossbreed
  • mutate

As all of generate, crossbreed and mutate call 'evaluate the fitness'. it is the busiest function and is the primary bottleneck speedwise.

As is the nature of genetic algorithms, it has to search an immense solution space so the faster the better. I want to speed up each of these functions. I'll start with the fitness evaluator. My question is what is the best way to do this. I've been looking into cython, ctypes and 'linking and embedding'. They are all new to me and quite beyond me at the moment but I look forward to learning one and eventually all of them.

The 'fitness function' needs to compare the value of the expression tree to the value of the target expression. So it will consist of a postfix evaluator which will read the tree in a postfix order. I have all the code in python.

I need advice on which I should learn and use now: cython, ctypes or linking and embedding.

Thank you.

© Stack Overflow or respective owner

Related posts about python

Related posts about cython