2

I'm writing a small toy simulation in python. Granted, this simulations are slow. To my understanding, the major reason that python codes are slow is the fact that python is in interpreted language. I don't want to give up python since the clear syntax and the available library cut the writing time significantly. So is there a simple way for me to "compile" my python code?

Edit

I answer some questions: Yes, I'm using numpy. It greatly simplify the code and I don't think I can improve performance writing the functions on my own. I use numpy for all my lists and and I add all of the beads together. Namely. I invoke

pos += V*dt + forces*0.5*dt**2 

where ''pos'', 'V', and 'forces' are all np.array of (2000,3) dimensions. I'm quite certain that the slow part in the forces calculation. This is logical as I have to iterate over all my particles and check their position. For my real project (Ph.D. stuff) I have code of about roughly the same level of complexity, and I know that this is the expensive stuff.

9
  • 3
    Are you using numpy for your calculations? You could also try PyPy which is a faster python interpreter than the "default" one written in C. Commented Jan 31, 2014 at 7:55
  • 1
    Also, different ways of writing python can have large performance differences. There are also C extensions that can help in some areas. Can you show us some code? Commented Jan 31, 2014 at 7:55
  • 3
    @Gjordis this does not really make sense. py_compile only manually does what every python-invocation does with source code - namely byte-compiling it. Commented Jan 31, 2014 at 7:55
  • 3
    Have you profiled your application to figure out where its actually slow? Just "compiling it" may not solve your problem. If its stuck on I/O running it on the fastest compiled language won't help. Commented Jan 31, 2014 at 8:05
  • 2
    Definitely profile your code. For the snippet you've shown I would consider trying github.com/pydata/numexpr Commented Jan 31, 2014 at 8:32

2 Answers 2

3

If none of the solutions in the comment suffice, you can also take a look at cython. For a quick tutorial & example check:

http://docs.cython.org/src/tutorial/cython_tutorial.html

Used at the correct spots (e.g. around frequently called functions) it can easily speed things up by a factor of 10 - 100.

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

Comments

1

Python is a slightly odd language in that it is both interpreted and compiled. Well sort of. When you run it is compiled to ".pyc" bytecode - so we can quickly get bogged down in semantic details here. Hell I don't even know if what I just said is strictly accurate. But at the end of the day you want to speed things up so...

  1. First, use the profiler and timeit to work out where all the time is going
  2. Second, rewrite your pure python code to improve the slow bits you've discovered
  3. Third, see how it goes when optimised
  4. Now, depends on your scenario, but seriously think "Can I run it on a bigger CPU/memory"
  5. Ok, try rewriting those slow sections in C++
  6. Screw it, write it all in C++

If you get so far as the last option I dare say you're screwed and the savings aren't going to be significant.

2 Comments

Nothing "odd" about Python -- that's how many if not most programming languages work: source code gets parsed and transformed into bytecode which is then executed... sometimes the bytecode is machine specific in which case we call the result a native binary, sometimes the result requires a bytecode interpreter better known as a virtual machine.
Well, profiling my code, I learn that I call numpy.zeros sh*$load of times (actual cProfile output) with cumtime of 5.69 seconds(?) out of total of 78.108 seconds. So if there anything to reduce, for now, it is there. Can I improve it by doing this manually? I don't think so since the ''percall'' value is 0

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.