ODISSEI-School-Choice / school-choice

This Python implementation tries to model school choice and resulting school segregation based on the work of Schelling (1971) and Stoica & Flache (2014).
Apache License 2.0
0 stars 0 forks source link

Optimize performance of the calc_school_rankings function in the model.py module #7

Closed jiqicn closed 2 years ago

jiqicn commented 2 years ago

Possible bottlenecks:

jiqicn commented 2 years ago

The expensive parts of the code (by order):

Both the first and second parts are quite straight forward (vectorized operations), and no idea how to make them faster.

For the third part that is the for-loop, tried to vectorize things as much as possible, but still no significant improvement on performance (typically about 1 second faster).

See code:

https://github.com/ODISSEI-School-Choice/school-choice/blob/92cea49628a8d670c6f8d651315bc5a987e458ae/compass/origin/model.py#L622

jiqicn commented 2 years ago

TODO:

jiqicn commented 2 years ago

The Cython code can be found in the gitlabmaster branch: https://github.com/ODISSEI-School-Choice/school-choice/tree/gitlabmaster/cython_school_ranking.

The idea is to type all the variables and accept numpy arrays involved in heavy computation as memoryviews. Also, the argsort function is rewritten in Cython as well.

There is a notebook to show the profiling result of both the Python and the Cython versions. Basically, I would say they are equally fast, which means converting Python code to Cython doesn't bring significant improvement on performance. And since it doesn't add any benefit on readability of the code, the Cython solution is given up.