From b0ba9854ae1310425d7c09c72c528239a001e7c7 Mon Sep 17 00:00:00 2001 From: Xia Wang Date: Thu, 19 Nov 2009 16:28:25 -0800 Subject: [PATCH] Add standard deviation to the test results --- tests/sdcard/stopwatch.cpp | 20 +++++++++++++++----- tests/sdcard/stopwatch.h | 1 + 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/tests/sdcard/stopwatch.cpp b/tests/sdcard/stopwatch.cpp index 8207430d..77768d69 100644 --- a/tests/sdcard/stopwatch.cpp +++ b/tests/sdcard/stopwatch.cpp @@ -31,6 +31,7 @@ #include #include #include "stopwatch.h" +#include #define SNPRINTF_OR_RETURN(str, size, format, ...) { \ int len = snprintf((str), (size), (format), ## __VA_ARGS__); \ @@ -53,7 +54,7 @@ namespace android_test { StopWatch::StopWatch(const char *name, size_t capacity) : mName(strdup(name)), mNum(0), mData(NULL), mDataLen(0), mCapacity(capacity * 2), mSizeKbytes(0), mAlreadyPrinted(false), mPrintRaw(false), - mDuration(0.0), + mDuration(0.0), mDeviation(0.0), mMinDuration(0.0), mMinIdx(0), mMaxDuration(0.0), mMaxIdx(0), mDeltas(NULL), mUsed(false) @@ -126,7 +127,7 @@ void StopWatch::sprint(char **str, size_t *size) { // print comment header and summary values. - SNPRINTF_OR_RETURN(*str, *size, "# Name Iterations Duration Min MinIdx Max MaxIdx SizeMbytes\n"); + SNPRINTF_OR_RETURN(*str, *size, "# Name Iterations Duration Min MinIdx Max MaxIdx SizeKbytes\n"); SNPRINTF_OR_RETURN(*str, *size, "%s %d %f %f %d %f %d %d\n", mName, mNum, mDuration, mMinDuration, mMinIdx, mMaxDuration, mMaxIdx, mSizeKbytes); // print each duration sample @@ -164,8 +165,8 @@ void StopWatch::checkCapacity() void StopWatch::processSamples() { if (kVerbose) fprintf(stderr, "processing samples\n"); - mDeltas= new double[mDataLen / 2]; - + size_t n = mDataLen / 2; + mDeltas= new double[n]; for (size_t i = 0; i < mDataLen; i += 2) // even: start odd: stop { long second = mData[i + 1].mTime.tv_sec - mData[i].mTime.tv_sec; @@ -174,7 +175,7 @@ void StopWatch::processSamples() mDeltas[i / 2] = double(second) + double(nano) / 1.0e9; } - for (size_t i = 0; i < mDataLen / 2; ++i) + for (size_t i = 0; i < n; ++i) { if (0 == i) { @@ -195,8 +196,16 @@ void StopWatch::processSamples() } mDuration += mDeltas[i]; } + double avgDuration = mDuration / n; + double diffSQ = 0.0; + for (size_t i = 0; i < n; ++i) + { + diffSQ += pow((mDeltas[i] - avgDuration), 2.0); + } + mDeviation = sqrt(diffSQ / n); } + double StopWatch::timespecToDouble(const struct timespec& time) { double val = double(time.tv_nsec) / 1.0e9 + double(time.tv_sec); @@ -210,6 +219,7 @@ void StopWatch::printAverageMinMax(char **str, size_t *size) if (mDataLen > 2) // if there is only one sample, avg, min, max are trivial. { SNPRINTF_OR_RETURN(*str, *size, "# Average %s duration %f s/op\n", mName, mDuration / mNum); + SNPRINTF_OR_RETURN(*str, *size, "# Standard deviation %s duration %f \n", mName, mDeviation); SNPRINTF_OR_RETURN(*str, *size, "# Min %s duration %f [%d]\n", mName, mMinDuration, mMinIdx); SNPRINTF_OR_RETURN(*str, *size, "# Max %s duration %f [%d]\n", mName, mMaxDuration, mMaxIdx); } diff --git a/tests/sdcard/stopwatch.h b/tests/sdcard/stopwatch.h index 4d1a7949..c54f2576 100644 --- a/tests/sdcard/stopwatch.h +++ b/tests/sdcard/stopwatch.h @@ -141,6 +141,7 @@ class StopWatch { bool mPrintRaw; double mDuration; + double mDeviation; double mMinDuration; size_t mMinIdx; double mMaxDuration;