How can I 'transform' Class object in python to llvm?

300 Views Asked by At

I am willing to use llvm to optimize my python code. I read some tutorials but I have not figured out how to, let's say, export class object from the python code to llvm. I learnt how to create a function in llvm-py, but classes are beyond my forces.

Here is an example of class I want to create in llvm code:

class Char():
    def __init__(self,c):
        self.c=c
    def next(self,line,p):
        try:
            return self.c==line[p]
        except:
            return False
    def next_rep(self,line,p):
        try:
            return self.c==line[p],p
        except:
            return False,p

I would be grateful for any help!

1

There are 1 best solutions below

0
On BEST ANSWER

Short answer: you can't.

The reason is that Python is an interpreted language, and there are several statements in the language that won't easily lend to static evaluation.

My suggestion is that you profile your program (for example, if you're running Linux use IPython's run -p option, or in general through the cProfile module), and figure out what's taking the bulk of the program's time.

In most programs, a high percentage of the total running time is taken by a relatively small area of code, and improving it (either through an algorithmic improvement or through writing a C extension, for example through SWIG) can often result in an order of magnitude improvement in performance.

This kind of optimization is usually much more effective than trying to make "everything run faster".