1

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!

7
  • 1
    A cursory investigation suggests that llvm-py is just a tool for creating llvm code with some python niceties. It's not a python compiler. Maybe look at pypy, which had an llvm backend. This fork seems to still be under active development and have some code there. Commented Jul 10, 2012 at 13:16
  • Thanks for your suggestion but the thing is, that I am generating deterministic finite automatons and I want those automatons to be faster. So I thought that would be nice to generate them in llvm bitcode. And I want to use them in python code... So maybe I should be asking - how can I rewrite that using a llvm-py to llvm bitcode. Commented Jul 10, 2012 at 13:47
  • why llvm? Where is the bottleneck? Commented Jul 10, 2012 at 14:26
  • Well, I just thought that llvm would be a good way to make my computations faster and given that I am working with deterministic finite automata it seemed to be the right way. Commented Jul 11, 2012 at 6:27
  • 1
    Why not try a simpler solution like Cython? It will allow you to trivially write a C module. Commented Jul 11, 2012 at 7:33

1 Answer 1

1

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".

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.