Skip to content

Commit

Permalink
Fix performance issue with nanstd and nanvar
Browse files Browse the repository at this point in the history
  • Loading branch information
zhujun98 committed Apr 7, 2021
1 parent 78aaac3 commit 622647d
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions include/xtensor/xmath.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2664,8 +2664,8 @@ namespace detail {
XTL_REQUIRES(is_reducer_options<EVS>)>
inline auto nanvar(E&& e, EVS es = EVS())
{
decltype(auto) sc = detail::shared_forward<E>(e);
return nanmean<T>(square(sc - nanmean<T>(sc)), es);
auto cached_mean = nanmean<T>(e, es)();
return nanmean<T>(square(std::forward<E>(e) - std::move(cached_mean)), es);
}

template <class T = void, class E, class EVS = DEFAULT_STRATEGY_REDUCERS,
Expand Down Expand Up @@ -2702,7 +2702,8 @@ namespace detail {
// note: forcing copy of first axes argument -- is there a better solution?
auto axes_copy = axes;
using result_type = typename std::conditional_t<std::is_same<T, void>::value, double, T>;
auto inner_mean = nanmean<result_type>(sc, std::move(axes_copy));
// always eval to prevent repeated evaluations in the next calls
auto inner_mean = eval(nanmean<result_type>(sc, std::move(axes_copy), evaluation_strategy::immediate));

// fake keep_dims = 1
auto keep_dim_shape = e.shape();
Expand Down

0 comments on commit 622647d

Please sign in to comment.