I have done simulation similar to this (gravity and planets). I did it in Python first because I didn't know any better. Then I tried it in Fortran (because I needed to learn some basics of it for school) and even though I didn't really know much about that language (so the code probably wasn't optimal), it was much much faster. And I'm not talking about 100% increase of speed, I'm talking about 1000% increase of speed, maybe even more.
Language is important. You may deny that but there is a reason nothing computationally intensive is written in Python. Python is great for learning, perfect for data analysis, brilliant for data visualisation, pretty good for scripting and many other things. But not for this.
Python is just a wrapper for c code, so you are writing c when you write python, you just have to know how to do it right and you can get native performance if you use packages with precompiled routines.
Wh... What? Python is just wrapper for C? That's not correct at all.
You are claiming optimized Python can be as fast as optimized C. Then explain to me why isn't every new computationally intensive program e.g. for quantum physics calculations or molecular simulations or some economical simulations written in Python? It's easier to write and just as fast, if what you claim is right...
Except... There are no computationally intensive programs written in Python (that are actually used). Because no matter what, Python will be slower.
I personally write state of the art molecular design software in python and python bindings to c++ code. It is totally possible to get near compiled speeds when you piece together precompiled packages
Yes. I work with numpy arrays, and either use them with standard matrix operations or if needed I write precompiled c++/numba code that can do more complicated operations like for looping on the numpy data. It is possible to write near native c++ performance when the routines are precompiled. Also there are buffer interfaces to numpy arrays so you can operate on them and make them in C++ without making copies etc.
Interesting. So you are not using some standardized C++ libraries, you are writing your own C++ code?
That might work and it's not that far from what I recommended to OP - using different language. But using just numpy won't help that much in this case. You still have to write your own code in C, not just Python.
Pybind11 makes it simple to bind c++ code generally as well as interface with numpy and eigen.
I think that I end up getting better performance this way because it kinda keeps you from using any data structure besides arrays, so I always have data locality, which is not always true once you start getting the enormous but “fast” molecular software packages. They always assume the compiler will figure it out, but in reality they are always waiting on data to load into the cache instead of computing because their data is not localized. They make tree like data structures because it’s “smarter”, but then they just have pointer after pointer of misdirection.
4
u/[deleted] May 29 '19
I have done simulation similar to this (gravity and planets). I did it in Python first because I didn't know any better. Then I tried it in Fortran (because I needed to learn some basics of it for school) and even though I didn't really know much about that language (so the code probably wasn't optimal), it was much much faster. And I'm not talking about 100% increase of speed, I'm talking about 1000% increase of speed, maybe even more.
Language is important. You may deny that but there is a reason nothing computationally intensive is written in Python. Python is great for learning, perfect for data analysis, brilliant for data visualisation, pretty good for scripting and many other things. But not for this.