2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "frame-time-stats.h"
35 const float EPSILON = 0.9f; // rolling average = (average * epsilon) + (current * epsilon)
36 const float ONE_OVER_MICROSECONDS_TO_SECONDS = 1.f / 1000000.f; ///< microseconds per second
39 FrameTimeStats::FrameTimeStats()
42 mSamples.Reserve( 16 ); // Fill out a little to avoid early reallocations
47 FrameTimeStats::~FrameTimeStats()
51 void FrameTimeStats::StartTime( const FrameTimeStamp& timeStamp )
53 // check to make sure we don't get 2 start times in a row
54 if( mTimeState != WAITING_FOR_START_TIME )
60 mTimeState = WAITING_FOR_END_TIME;
63 void FrameTimeStats::EndTime( const FrameTimeStamp& timeStamp )
65 if( mTimeState != WAITING_FOR_END_TIME )
71 mTimeState = WAITING_FOR_START_TIME;
74 // frame time in seconds
75 unsigned int elapsedTime = FrameTimeStamp::MicrosecondDiff( mStart, timeStamp);
77 mSamples.PushBack( elapsedTime );
79 // if the min and max times haven't been set, do that now.
84 mMinMaxTimeSet = true;
88 if (elapsedTime < mMin)
92 else if (elapsedTime > mMax)
98 mTotal += elapsedTime;
101 void FrameTimeStats::Reset()
103 mTimeState = WAITING_FOR_START_TIME;
104 mMinMaxTimeSet = false;
111 float FrameTimeStats::GetMaxTime() const
113 return mMax * ONE_OVER_MICROSECONDS_TO_SECONDS;
116 float FrameTimeStats::GetMinTime() const
118 return mMin * ONE_OVER_MICROSECONDS_TO_SECONDS;
121 float FrameTimeStats::GetTotalTime() const
123 return mTotal * ONE_OVER_MICROSECONDS_TO_SECONDS;
126 unsigned int FrameTimeStats::GetRunCount() const
131 void FrameTimeStats::CalculateMean( float& meanOut, float& standardDeviationOut ) const
133 if( mSamples.Size() > 0 )
136 unsigned int sum = 0;
137 for( Samples::ConstIterator it = mSamples.Begin(), itEnd = mSamples.End(); it != itEnd; ++it )
139 unsigned int value = *it;
144 meanOut = static_cast<float>(sum) / mSamples.Size();
147 float variance = 0.0f;
148 for( Samples::ConstIterator it = mSamples.Begin(), itEnd = mSamples.End(); it != itEnd; ++it )
150 unsigned int value = *it;
152 float difference = static_cast<float>(value) - meanOut;
154 variance += difference * difference;
157 variance /= mSamples.Size();
159 // Standard deviation
160 standardDeviationOut = sqrtf( variance );
162 meanOut *= ONE_OVER_MICROSECONDS_TO_SECONDS;
163 standardDeviationOut *= ONE_OVER_MICROSECONDS_TO_SECONDS;
168 standardDeviationOut = 0.0f;
173 } // namespace Adaptor
175 } // namespace Internal