Skip to content

Commit

Permalink
Optimized fast_array_util.py: Achieved ~30% speed increase for tardis…
Browse files Browse the repository at this point in the history
…-sn#2757

Implemented faster alternative code in fast_array_util.py
Achieved approximately 30% performance boost
Maintained same functionality and output accuracy

This optimization addressed the concerns raised in tardis-sn#2757.
Closes tardis-sn#2757
  • Loading branch information
hayrilatif committed Jul 31, 2024
1 parent c57d74d commit 0b782f9
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions tardis/plasma/properties/continuum_processes/fast_array_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,36 @@
from numba import njit, prange


@njit(**njit_dict)
def __faster_cumsum(arr):
"""Faster alternative for numpy's cumsum."""
result = np.empty_like(arr)
result[0] = arr[0]
for i in range(1, len(arr)):
result[i] = result[i - 1] + arr[i]
return result


@njit(**njit_dict)
def __faster_diff(arr):
"""Calculates x diffs."""
s = arr.shape[0]
result = np.empty((s - 1,))
for i in prange(0, s - 1):
result[i] = arr[i + 1] - arr[i]
return result


@njit(**njit_dict)
def __faster_add(arr):
"""Calculates trapezoid's total parallel length."""
s = arr.shape[0]
result = np.empty((s - 1,))
for i in prange(0, s - 1):
result[i] = arr[i + 1] + arr[i]
return result


@njit(**njit_dict)
def numba_cumulative_trapezoid(f, x):
"""
Expand All @@ -23,8 +53,8 @@ def numba_cumulative_trapezoid(f, x):
-------
numpy.ndarray, dtype float
The result of cumulative integration of f along x
"""
integ = (np.diff(x) * (f[1:] + f[:-1]) / 2.0).cumsum()
"""
integ = __faster_cumsum(__faster_diff(x) * __faster_add(f) / 2.0)
return integ / integ[-1]


Expand Down

0 comments on commit 0b782f9

Please sign in to comment.