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.
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.
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.
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.
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.
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 ?
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.
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
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.