Can I improve python runtime by compiling?

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.

Answers


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.


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.


Need Your Help

Readable C# equivalent of Python slice operation

c# python slice equivalent

What is the C# equivalent of Python slice operations?

How do I make just an image clickable and not the entire div?

html css image hyperlink

How do I go about making just an image clickable and not the entire div that the clickable image is inside of? In this case, the first image, .home, an anchor, and the div that contains it, .nav, is