Skip to content

Commit 86be631

Browse files
Speed up of cross_section 💨 (#245)
This speeds up calls to `cross_section` by a factor of 3 😮 There is some slow down of the cross section calculation described in #244 . This reduces the number of internal calls of `cross_section` due to `_temporary_kinematics` context manager. Before PR: ```python In [2]: from chromo.kinematics import FixedTarget ...: from chromo.models import Sibyll23e In [3]: kin_init = FixedTarget(100, 2212, 'O16') ...: kin = FixedTarget(80, 2212, 'O16') ...: ...: ...: sib = Sibyll23e(kin_init) In [4]: def cs(mod, kin): ...: return mod.cross_section(kin).prod ...: In [5]: %timeit cs(sib, kin) 992 μs ± 252 ns per loop (mean ± std. dev. of 7 runs, 1,000 loops each) ``` With this PR: ```python In [8]: %timeit cs(sib, kin) 328 μs ± 791 ns per loop (mean ± std. dev. of 7 runs, 1,000 loops each) ``` --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 22e638a commit 86be631

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

‎src/chromo/common.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ def cross_section(self, kin=None, max_info=False):
810810
kin3.p2 = component
811811
# this calls cross_section recursively, which is fine
812812
cross_section._mul_radd(
813-
fraction, self._cross_section(kin3, max_info=max_info)
813+
fraction, self.cross_section(kin3, max_info=max_info)
814814
)
815815
return cross_section
816816
return self._cross_section(kin, max_info=max_info)
@@ -1010,6 +1010,10 @@ def _temporary_kinematics(self, kin):
10101010
yield
10111011
else:
10121012
prev = copy.copy(self.kinematics)
1013-
self.kinematics = kin
1013+
self._check_kinematics(kin)
1014+
self._kinematics = kin
1015+
self._set_kinematics(kin)
10141016
yield
1015-
self.kinematics = prev
1017+
# _check_kinematics not necessary because it's the old kinematics
1018+
self._kinematics = prev
1019+
self._set_kinematics(prev)

0 commit comments

Comments
 (0)