Skip to content

Commit

Permalink
Improve and measure speed of clamp function
Browse files Browse the repository at this point in the history
Change the comment at the start of the clamp
function because the comparison between the clip
function and the alternatives has been made.
Substitute the clamp function with the most
performant alternative.
  • Loading branch information
Tortar authored and rht committed Oct 9, 2022
1 parent 478a66b commit fed8354
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions mesa/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@


def clamp(x: float, lowest: float, highest: float) -> float:
# This should be faster than np.clip for a scalar x.
# TODO: measure how much faster this function is.
return max(lowest, min(x, highest))
# much faster than np.clip for a scalar x.
return lowest if x <= lowest else (highest if x >= highest else x)


def accept_tuple_argument(wrapped_function: F) -> F:
Expand Down

0 comments on commit fed8354

Please sign in to comment.