average timespecs
I have a stl vector of timespecs and need to determine the average of the timespecs. The size of the vector wont be any larger than 5k.
What I have doesn't seem to work quite right. I'm thinking the data types may be rolling over. Anyone got any ideas?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
static unsigned long CalculateAvg(vector<timespec> *pvecResults){
timespec sumTime;
unsigned long ulAvgNs = 0;
unsigned long ultotalNs = 0;
try
{
// Build up the result statistics. First we need the sum the execTimes of each call
for(vector<timespec>::iterator vecResultsIterator = pvecResults->begin();
vecResultsIterator != pvecResults->end();
vecResultsIterator++)
{
sumTime = tsAdd(sumTime,(timespec)*vecResultsIterator);
}
// then convert sumTime to nanoseconds
ultotalNs = sumTime.tv_sec * NSEC_PER_SEC + sumTime.tv_nsec;
// Then calculate the average nanoseconds/session
ulAvgNs = ultotalNs / pvecResults->size();
}
catch(...)
{
cout << "Error calculating average" << endl;
}
return ulAvgNs;
}
| |
How about just adding the nanoseconds instead of the timespecs?
1 2 3 4 5 6 7 8 9 10
|
unsigned long ulAvgNs = 0;
unsigned long ultotalNS = 0;
for(vector<timespec>::iterator vecResultsIterator = pvecResults->begin();
vecResultsIterator != pvecResults->end(); vecResultsIterator++)
{
ultotalNS += vecResultsIterator->tv_sec * NSEC_PER_SEC + vecResultsIterator->tv_nsec;
}
ulAvgNs = ultotalNs / pvecResults->size();
| |
You don't appear to initialize sumTime.
Topic archived. No new replies allowed.