icecube / skyllh

https://icecube.github.io/skyllh/
GNU General Public License v3.0
11 stars 5 forks source link

Implement own version of numpy.random.choice for better performance #171

Closed martwo closed 1 year ago

martwo commented 1 year ago

Profiling of SkyLLH concluded that much time is spend in the numpy.random.choice function when generating signal events. The code of that function is here: https://github.com/numpy/numpy/blob/20d03bcfd9864527bf70cf3747ade162d2034f68/numpy/random/mtrand.pyx#L952-L995

Since the MC event probabilities do not change between trials, one could save the cumulative distribution function (cdf) and reuse it. Also there is no need for value checking of the probabilities each call. This should speed up signal generation.

I propose a small RandomChoice class which holds the CDF of the signal candidates: https://github.com/icecube/skyllh/blob/074c50891a7b9ecedcb5fef65af6a5b7126cb1b5/skyllh/core/signal_generator.py#L645