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!

Jendas
  • 3,359
  • 3
  • 27
  • 55
  • 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](http://pypy.org/), which had an llvm backend. [This fork](https://bitbucket.org/mjacob/pypy-llvm/src/bbae9b5448ef/pypy/translator/llvm/genllvm.py) seems to still be under active development and have some code there. – Henry Gomersall Jul 10 '12 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. – Jendas Jul 10 '12 at 13:47
  • why llvm? Where is the bottleneck? – Henry Gomersall Jul 10 '12 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. – Jendas Jul 11 '12 at 06:27
  • 1
    Why not try a simpler solution like [Cython](http://www.cython.org/)? It will allow you to trivially write a C module. – Henry Gomersall Jul 11 '12 at 07:33
  • I thought about that but the Cython solution would be slower the llvm wouldn't be? Speed of the program is the priority No. 1, so if llvm, is going to do the work faster I have to try write it using llvm even though it will be much more complicated – Jendas Jul 11 '12 at 07:57
  • Well, that depends on how you write it! You can do anything in Cython, since you can call any libraries. By your same argument you should write it in CUDA or OpenCL (assuming it vectorises, which you imply it will given you think LLVM will be much faster than Cython). – Henry Gomersall Jul 11 '12 at 18:35

1 Answers1

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

Guy Adini
  • 5,188
  • 5
  • 32
  • 34