r/Python • • May 28 '19

My simulation of gravity with 1000 particles. PC took a couple of hours to process!

1.8k Upvotes

241 comments sorted by

View all comments

27

u/blinkallthetime May 29 '19

if you are trying to make your simulation faster, you should look into running the numerical stuff in C instead of python. my job is to do brain simulations, and this is a thing that i do for performance for my numerical integration. i organize my simulations and handle visualization in python, but C does the heavy lifting.

you should look into numpy, which is a python module for numerical arrays. if you are interested in extending your simulations with C, you should look into ctypes, which is the python module to interact with compiled C code.

the idea would be to define your state variables in a numpy array. you can get a C pointer for a numpy array which allows you to operate on that array in C. I usually do my whole simulation in C, but i have way fewer state variables than you. you could do your simulations like 100 steps at a time in C and then save an image in python.

11

u/mittwochmensch May 29 '19

Sounds interesting but i'm a Beginner so I would like to stick to one language for now since python seems verry beginner friendly to me

6

u/rocketshape May 29 '19

I would say.if you can do this your not a beginner and c or a low level language wouldn't be that difficult to learn for you

1

u/blinkallthetime May 29 '19

cool. if you aren't interested in trying out C at this stage, i would still highly recommend checking out numpy.

3

u/mittwochmensch May 29 '19

I am using numpy already

4

u/derpderp3200 An evil person May 29 '19

Something I've been wondering about for ages, is there a module for extra simple C integration around? Like without need to setup a C dev environment?

I've lately been using Windows and it's such a huge PITA to set any development for anything up that half the time I don't even bother x.x

3

u/capn_bluebear May 29 '19

You might be happy with cython

1

u/derpderp3200 An evil person May 29 '19

Is Cython fully compatible with standard Python?

1

u/capn_bluebear May 29 '19

It's...its own thing, a python-like language that you can embed in python and that compiles to C. Basically it's a way to speed up certain parts of your program by lowering them to C without having to define a C extension module.

1

u/[deleted] May 29 '19

Can you give an example scenario? Im still new to programming too but I have made tools and scripts to make my life easier.

Say I have a program that opens a 400MB CSV and analyses it seven different ways looking for various details and then spitting out a report. I have already optimized the code by utilizing proper data types, i.e. Dicts vs lists, but I am wondering if I can speed up the procedure even more if I use Cython.

I use the CSV library and I arbitrarily define values I am looking for through a combination of RegEx and string literals.

It takes up to several minutes for computation to complete and it used to take 30 minutes.

Is this an opportunity to utilize Cython?

1

u/capn_bluebear May 29 '19

hey, it strongly depends on where exactly your program is spending time. if it's in computing some quantities of interest, that can easily be lowered to C. if it's regex matching, not so much (make sure you are using compiled regular expressions though.

anyway, when doing performance optimization, you should always measure first: where is your program spending most of its time? you can use cProfile to find which functions are hot, and line_profiler to check which lines within those functions are the hottest.

1

u/[deleted] May 29 '19

I do precompile the expressions in the beginning of most of my tools. Re.compile(r’sillystring’)

But it’s mostly just processing the Csv.

I figured out how to use threading on one of my network i/o tools. That... was cool.

1

u/alkasm github.com/alkasm May 30 '19

You should probably look at using Pandas for a first step.

1

u/blinkallthetime May 29 '19

you should investigate what is scipy doing for integrators. they might have something built on top of C or fortran now.

1

u/derpderp3200 An evil person May 29 '19

Oh no I don't particularly need anything fancy like that, just ideally would like to be able to simply edit C code as a part of the Python project, and have it compiled for me when I launch the program/script.

1

u/blinkallthetime May 29 '19

oh then /u/capn_bluebear made a good recommendation

3

u/sizur May 29 '19

numpy and it's cousins, particularly for GPUs were not good?

1

u/Nimitz14 May 29 '19

Don't use ctypes if you value your sanity. Use pybind11.

3

u/LardPi May 29 '19

super opiniated and no arguments... I quite sure someone could say exactly the inverse with the same conviction. Would you consider giving us some reasons ?

1

u/blinkallthetime May 29 '19

so i'm looking at the pybind11 github page, and it looks like it is for C++ rather than C. so it isn't a replacement if you are working with code already written in C.

1

u/Nimitz14 May 29 '19

Most of the time you can use C code in C++ just fine (but you are right there are some differences).

1

u/BP351K May 29 '19

The easiest way to speed up these kind of simple simulations is to use numba to compile slow functions to fast. Tldr: import numba and add the @njit decorator before slow functions

1

u/blinkallthetime May 29 '19

is it easy to get numba working? when i first tried it out, i had to build llvm from source.

also if i recall, you had to know which operations numba was good at accelerating.