From a0299cc63aecdd668ee35785661f5cb42e3bc831 Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Thu, 14 Mar 2019 01:23:37 +0100 Subject: [PATCH] Optimize calculation of standard deviation. --- server/TracyView.cpp | 10 +++------- server/TracyWorker.cpp | 2 ++ server/TracyWorker.hpp | 1 + 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/server/TracyView.cpp b/server/TracyView.cpp index ca2b8ec2..60ac721a 100644 --- a/server/TracyView.cpp +++ b/server/TracyView.cpp @@ -6029,14 +6029,10 @@ void View::DrawFindZone() TextFocused( "Median time:", TimeToString( m_findZone.median ) ); if( m_findZone.sorted.size() > 1 ) { + const auto sz = m_findZone.sorted.size(); const auto avg = m_findZone.average; - double ss = 0; - for( auto& v : m_findZone.sorted ) - { - const auto d = double( v ) - avg; - ss += d*d; - } - const auto sd = sqrt( ss / ( m_findZone.sorted.size() - 1 ) ); + const auto ss = zoneData.sumSq - 2. * zoneData.total * avg + avg * avg * sz; + const auto sd = sqrt( ss / ( sz - 1 ) ); ImGui::SameLine(); ImGui::Spacing(); diff --git a/server/TracyWorker.cpp b/server/TracyWorker.cpp index dddc7b7a..8f89d17d 100644 --- a/server/TracyWorker.cpp +++ b/server/TracyWorker.cpp @@ -2590,6 +2590,7 @@ void Worker::ProcessZoneEnd( const QueueZoneEnd& ev ) slz.min = std::min( slz.min, timeSpan ); slz.max = std::max( slz.max, timeSpan ); slz.total += timeSpan; + slz.sumSq += double( timeSpan ) * timeSpan; if( zone->child >= 0 ) { for( auto& v : GetZoneChildren( zone->child ) ) @@ -3587,6 +3588,7 @@ void Worker::ReadTimelineUpdateStatistics( ZoneEvent* zone, uint16_t thread ) slz.min = std::min( slz.min, timeSpan ); slz.max = std::max( slz.max, timeSpan ); slz.total += timeSpan; + slz.sumSq += double( timeSpan ) * timeSpan; if( zone->child >= 0 ) { for( auto& v : GetZoneChildren( zone->child ) ) diff --git a/server/TracyWorker.hpp b/server/TracyWorker.hpp index 9c2c1123..4847d0b7 100644 --- a/server/TracyWorker.hpp +++ b/server/TracyWorker.hpp @@ -86,6 +86,7 @@ private: int64_t min = std::numeric_limits::max(); int64_t max = std::numeric_limits::min(); int64_t total = 0; + double sumSq = 0; int64_t selfMin = std::numeric_limits::max(); int64_t selfMax = std::numeric_limits::min(); int64_t selfTotal = 0;