diff --git a/examples/Evolution/NN3DWalkers.cpp b/examples/Evolution/NN3DWalkers.cpp index 497621428..3ca2b9c83 100755 --- a/examples/Evolution/NN3DWalkers.cpp +++ b/examples/Evolution/NN3DWalkers.cpp @@ -15,9 +15,6 @@ subject to the following restrictions: #include "NN3DWalkers.h" -// not allowed declarations -#include - 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 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() diff --git a/examples/Utils/b3Clock.cpp b/examples/Utils/b3Clock.cpp index 8e1884526..1990eb46b 100644 --- a/examples/Utils/b3Clock.cpp +++ b/examples/Utils/b3Clock.cpp @@ -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(¤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) { #ifdef _WIN32 diff --git a/examples/Utils/b3Clock.h b/examples/Utils/b3Clock.h index 10a36686a..8bc511eb7 100644 --- a/examples/Utils/b3Clock.h +++ b/examples/Utils/b3Clock.h @@ -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);