Skip to content

Commit 622647d

Browse files
committed
Fix performance issue with nanstd and nanvar
1 parent 78aaac3 commit 622647d

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

include/xtensor/xmath.hpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -2664,8 +2664,8 @@ namespace detail {
26642664
XTL_REQUIRES(is_reducer_options<EVS>)>
26652665
inline auto nanvar(E&& e, EVS es = EVS())
26662666
{
2667-
decltype(auto) sc = detail::shared_forward<E>(e);
2668-
return nanmean<T>(square(sc - nanmean<T>(sc)), es);
2667+
auto cached_mean = nanmean<T>(e, es)();
2668+
return nanmean<T>(square(std::forward<E>(e) - std::move(cached_mean)), es);
26692669
}
26702670

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

27072708
// fake keep_dims = 1
27082709
auto keep_dim_shape = e.shape();

0 commit comments

Comments
 (0)