Extend b3Clock to expose the system current time in milliseconds. Replace #include time.h with b3Clock.
This commit is contained in:
@@ -15,9 +15,6 @@ subject to the following restrictions:
|
|||||||
|
|
||||||
#include "NN3DWalkers.h"
|
#include "NN3DWalkers.h"
|
||||||
|
|
||||||
// not allowed declarations
|
|
||||||
#include <time.h>
|
|
||||||
|
|
||||||
class btBroadphaseInterface;
|
class btBroadphaseInterface;
|
||||||
class btCollisionShape;
|
class btCollisionShape;
|
||||||
class btOverlappingPairCache;
|
class btOverlappingPairCache;
|
||||||
@@ -33,6 +30,7 @@ class NNWalker;
|
|||||||
#include "LinearMath/btHashMap.h"
|
#include "LinearMath/btHashMap.h"
|
||||||
#include "../CommonInterfaces/CommonParameterInterface.h"
|
#include "../CommonInterfaces/CommonParameterInterface.h"
|
||||||
#include "../Utils/b3ReferenceFrameHelper.hpp"
|
#include "../Utils/b3ReferenceFrameHelper.hpp"
|
||||||
|
#include "../Utils/b3Clock.h"
|
||||||
#include "../RenderingExamples/TimeSeriesCanvas.h"
|
#include "../RenderingExamples/TimeSeriesCanvas.h"
|
||||||
#include "NN3DWalkersTimeWarpBase.h"
|
#include "NN3DWalkersTimeWarpBase.h"
|
||||||
|
|
||||||
@@ -136,7 +134,6 @@ class NN3DWalkersExample : public NN3DWalkersTimeWarpBase
|
|||||||
btAlignedObjectArray<class NNWalker*> m_walkersInPopulation;
|
btAlignedObjectArray<class NNWalker*> m_walkersInPopulation;
|
||||||
|
|
||||||
TimeSeriesCanvas* m_timeSeriesCanvas; // A plotting canvas for the walker fitnesses
|
TimeSeriesCanvas* m_timeSeriesCanvas; // A plotting canvas for the walker fitnesses
|
||||||
|
|
||||||
public:
|
public:
|
||||||
NN3DWalkersExample(struct GUIHelperInterface* helper)
|
NN3DWalkersExample(struct GUIHelperInterface* helper)
|
||||||
:NN3DWalkersTimeWarpBase(helper),
|
:NN3DWalkersTimeWarpBase(helper),
|
||||||
@@ -149,7 +146,8 @@ public:
|
|||||||
m_nextReapedIndex(0),
|
m_nextReapedIndex(0),
|
||||||
m_timeSeriesCanvas(NULL)
|
m_timeSeriesCanvas(NULL)
|
||||||
{
|
{
|
||||||
srand(time(NULL));
|
b3Clock clock;
|
||||||
|
srand(clock.getSystemTimeMilliseconds());
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual ~NN3DWalkersExample()
|
virtual ~NN3DWalkersExample()
|
||||||
|
|||||||
@@ -47,6 +47,7 @@ struct b3ClockData
|
|||||||
#ifdef B3_USE_WINDOWS_TIMERS
|
#ifdef B3_USE_WINDOWS_TIMERS
|
||||||
LARGE_INTEGER mClockFrequency;
|
LARGE_INTEGER mClockFrequency;
|
||||||
DWORD mStartTick;
|
DWORD mStartTick;
|
||||||
|
LONGLONG mPrevMilliTime;
|
||||||
LONGLONG mPrevElapsedTime;
|
LONGLONG mPrevElapsedTime;
|
||||||
LARGE_INTEGER mStartTime;
|
LARGE_INTEGER mStartTime;
|
||||||
#else
|
#else
|
||||||
@@ -228,6 +229,59 @@ double b3Clock::getTimeInSeconds()
|
|||||||
return double(getTimeMicroseconds()/1.e6);
|
return double(getTimeMicroseconds()/1.e6);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Gets the system time in milliseconds
|
||||||
|
double b3Clock::getSystemTimeMilliseconds() {
|
||||||
|
|
||||||
|
#ifdef B3_USE_WINDOWS_TIMERS
|
||||||
|
LARGE_INTEGER currentTime;
|
||||||
|
QueryPerformanceCounter(¤tTime);
|
||||||
|
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(¤tTime, 0);
|
||||||
|
return (currentTime.tv_sec) * 1000 +
|
||||||
|
(currentTime.tv_usec) / 1000;
|
||||||
|
#endif //__CELLOS_LV2__
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
void b3Clock::usleep(int microSeconds)
|
void b3Clock::usleep(int microSeconds)
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
|
|||||||
@@ -28,6 +28,9 @@ public:
|
|||||||
/// the Clock was created.
|
/// the Clock was created.
|
||||||
double getTimeInSeconds();
|
double getTimeInSeconds();
|
||||||
|
|
||||||
|
/// Gets the system time in milliseconds
|
||||||
|
double getSystemTimeMilliseconds();
|
||||||
|
|
||||||
///Sleep for 'microSeconds', to yield to other threads and not waste 100% CPU cycles.
|
///Sleep for 'microSeconds', to yield to other threads and not waste 100% CPU cycles.
|
||||||
///Note that some operating systems may sleep a longer time.
|
///Note that some operating systems may sleep a longer time.
|
||||||
static void usleep(int microSeconds);
|
static void usleep(int microSeconds);
|
||||||
|
|||||||
Reference in New Issue
Block a user