Extend b3Clock to expose the system current time in milliseconds. Replace #include time.h with b3Clock.

This commit is contained in:
Benjamin Ellenberger 2016-11-08 22:16:08 +01:00
parent 1fc36d0a9f
commit a76187fea5
3 changed files with 60 additions and 5 deletions

View File

@ -15,9 +15,6 @@ subject to the following restrictions:
#include "NN3DWalkers.h"
// not allowed declarations
#include <time.h>
class btBroadphaseInterface;
class btCollisionShape;
class btOverlappingPairCache;
@ -33,6 +30,7 @@ class NNWalker;
#include "LinearMath/btHashMap.h"
#include "../CommonInterfaces/CommonParameterInterface.h"
#include "../Utils/b3ReferenceFrameHelper.hpp"
#include "../Utils/b3Clock.h"
#include "../RenderingExamples/TimeSeriesCanvas.h"
#include "NN3DWalkersTimeWarpBase.h"
@ -136,7 +134,6 @@ class NN3DWalkersExample : public NN3DWalkersTimeWarpBase
btAlignedObjectArray<class NNWalker*> m_walkersInPopulation;
TimeSeriesCanvas* m_timeSeriesCanvas; // A plotting canvas for the walker fitnesses
public:
NN3DWalkersExample(struct GUIHelperInterface* helper)
:NN3DWalkersTimeWarpBase(helper),
@ -149,7 +146,8 @@ public:
m_nextReapedIndex(0),
m_timeSeriesCanvas(NULL)
{
srand(time(NULL));
b3Clock clock;
srand(clock.getSystemTimeMilliseconds());
}
virtual ~NN3DWalkersExample()

View File

@ -47,6 +47,7 @@ struct b3ClockData
#ifdef B3_USE_WINDOWS_TIMERS
LARGE_INTEGER mClockFrequency;
DWORD mStartTick;
LONGLONG mPrevMilliTime;
LONGLONG mPrevElapsedTime;
LARGE_INTEGER mStartTime;
#else
@ -228,6 +229,59 @@ double b3Clock::getTimeInSeconds()
return double(getTimeMicroseconds()/1.e6);
}
/// Gets the system time in milliseconds
double b3Clock::getSystemTimeMilliseconds() {
#ifdef B3_USE_WINDOWS_TIMERS
LARGE_INTEGER currentTime;
QueryPerformanceCounter(&currentTime);
LONGLONG elapsedTime = currentTime.QuadPart;
// Compute the number of millisecond ticks elapsed.
unsigned long msecTicks = (unsigned long)(1000 * elapsedTime /
m_data->mClockFrequency.QuadPart);
// Check for unexpected leaps in the Win32 performance counter.
// (This is caused by unexpected data across the PCI to ISA
// bridge, aka south bridge. See Microsoft KB274323.)
unsigned long elapsedTicks = GetTickCount();
signed long msecOff = (signed long)(msecTicks - elapsedTicks);
if (msecOff < -100 || msecOff > 100)
{
// Adjust the starting time forwards.
LONGLONG msecAdjustment = b3ClockMin(msecOff *
m_data->mClockFrequency.QuadPart / 1000, elapsedTime -
m_data->mPrevMilliTime);
elapsedTime -= msecAdjustment;
// Recompute the number of millisecond ticks elapsed.
msecTicks = (unsigned long)(1000 * elapsedTime /
m_data->mClockFrequency.QuadPart);
}
// Store the current elapsed time for adjustments next time.
m_data->mPrevMilliTime = elapsedTime;
return msecTicks;
#else
#ifdef __CELLOS_LV2__
uint64_t freq = sys_time_get_timebase_frequency();
double dFreq = ((double)freq) / 1000.0;
typedef uint64_t ClockSize;
ClockSize newTime;
SYS_TIMEBASE_GET(newTime);
//__asm __volatile__( "mftb %0" : "=r" (newTime) : : "memory");
return (unsigned long int)((double(newTime)) / dFreq);
#else
struct timeval currentTime;
gettimeofday(&currentTime, 0);
return (currentTime.tv_sec) * 1000 +
(currentTime.tv_usec) / 1000;
#endif //__CELLOS_LV2__
#endif
}
void b3Clock::usleep(int microSeconds)
{
#ifdef _WIN32

View File

@ -28,6 +28,9 @@ public:
/// the Clock was created.
double getTimeInSeconds();
/// Gets the system time in milliseconds
double getSystemTimeMilliseconds();
///Sleep for 'microSeconds', to yield to other threads and not waste 100% CPU cycles.
///Note that some operating systems may sleep a longer time.
static void usleep(int microSeconds);