0

I wonder there are any tool to optimize my program in term of loop unrolling, and how can I use it?

I have the following python code:

for i in range(0, 1000):
       a = a * 10 + a%4 + i
for j in range(0, 1000):
       j = j + a 
for b in range(0, 1000):
      result = j + b

I want to optimize this code segment so that I can try to understand loop unrolling a bit. With Python, I want to know a C optimizer.

bfontaine
  • 18,169
  • 13
  • 73
  • 107

3 Answers3

4
a = 30
for i in range ( 0,1000 ) :
    a = a * 10 + a%4 + i

can be rewritten as:

a = reduce(lambda a,b: a * 10 + a%4 + b, xrange(1000), 30)

takes about the same time (~4ms on my computer).


for j in range ( 0, 1000 ) :
       j = j + a

doesn't make much sense. You are iterating j over 0-999, and each time add your huge a to it, which is immediately forgotten, because next j is taken. It can be rewritten as:

j = 999 + a

for b in range ( 0 , 1000 ) :
      result = j + b

doesn't make much sense either. It is equivalent to:

result = j + 999 
eumiro
  • 207,213
  • 34
  • 299
  • 261
3

If you aren't satisfied with the performance of your code, have profiled it, and found that low-level loops like this are a bottleneck, you should be able to speed up your code hugely by using cython to turn the expensive bits of code into C extensions. Also, if you are using python 2.x, you should be using xrange instead of range.

James
  • 3,191
  • 1
  • 23
  • 39
  • can you give example how can I use it ( cython ) –  Dec 14 '11 at 13:55
  • The official documentation is very good. Look at [getting started](http://docs.cython.org/src/quickstart/index.html) for instructions on installing cython, building cython code (using `pyximport` is the easiest way), calling it from python, and speeding up code using static type declarations. Instructions for `pyximport` are [here](http://docs.cython.org/src/userguide/source_files_and_compilation.html#compilation). – James Dec 14 '11 at 14:00
2

There exists a scientific paper regarding effects of loop unrolling in Python (pdf link). These are the slides of the related talk.

However, in terms of automatic C code optimization you can use LLVM in combination with LooPo and possibly Polly. Anyway, LLVM is a good starting point.

Sebastian
  • 8,046
  • 2
  • 34
  • 58
  • CAn you give example how to use it ( llvm ) –  Dec 14 '11 at 13:55
  • This is a course guide: Use LLVM to generate bytecode from your C-code (e. g. with `clang -emit-llvm -c foo.c -o foo.o`) and use one or more of [these](http://llvm.org/docs/Passes.html) transform passes. The result is again readable bytecode or even graphs (created with graphviz). You can give it a try, but you'll have to invest some time and don't expect miracles ;) – Sebastian Dec 14 '11 at 13:59
  • I searched this file with .deb format but I couldn't found any file in ubuntu package. Have you used them in ubuntu –  Dec 14 '11 at 14:04
  • You'll have to compile LLVM on your own. Read the documentation careful. Starting point is llvm.org – Sebastian Dec 14 '11 at 14:05