diff options
Diffstat (limited to 'graphics/asymptote/seconds.h')
-rw-r--r-- | graphics/asymptote/seconds.h | 144 |
1 files changed, 63 insertions, 81 deletions
diff --git a/graphics/asymptote/seconds.h b/graphics/asymptote/seconds.h index 0b92e902e2..a7ea9c0e59 100644 --- a/graphics/asymptote/seconds.h +++ b/graphics/asymptote/seconds.h @@ -1,99 +1,81 @@ #ifndef __seconds_h__ #define __seconds_h__ 1 -#ifdef _WIN32 -#include <Winsock2.h> +#include <chrono> -#include <time.h> -#include <windows.h> -#include <iostream> +namespace utils { -#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) -#define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64 +#ifdef _WIN32 +#include <Windows.h> +#define getpid GetCurrentProcessId +inline double cpuTime() { + FILETIME a,b,c,d; + return GetProcessTimes(GetCurrentThread(),&a,&b,&c,&d) != 0 ? + (double) (d.dwLowDateTime | + ((unsigned long long)d.dwHighDateTime << 32))*100.0 : 0.0; +} #else -#define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL +#include <unistd.h> +#include <time.h> +inline double cpuTime() { + timespec t; + clock_gettime(CLOCK_THREAD_CPUTIME_ID,&t); + return 1.0e9*t.tv_sec+t.tv_nsec; +} #endif -struct timezone -{ - int tz_minuteswest; /* minutes W of Greenwich */ - int tz_dsttime; /* type of dst correction */ +class stopWatch { + std::chrono::time_point<std::chrono::steady_clock> Start; + +public: + void reset() { + Start=std::chrono::steady_clock::now(); + } + + stopWatch() { + reset(); + } + + double nanoseconds(bool reset=false) { + auto Stop=std::chrono::steady_clock::now(); + double ns=std::chrono::duration_cast<std::chrono::nanoseconds> + (Stop-Start).count(); + if(reset) Start=Stop; + return ns; + } + + double seconds(bool reset=false) { + return 1.0e-9*nanoseconds(reset); + } }; -// Definition of a gettimeofday function - -inline int gettimeofday(struct timeval *tv, struct timezone *tz) -{ -// Define a structure to receive the current Windows filetime - FILETIME ft; - -// Initialize the present time to 0 and the timezone to UTC - unsigned __int64 tmpres = 0; - static int tzflag = 0; - - if (NULL != tv) - { - GetSystemTimeAsFileTime(&ft); - -// The GetSystemTimeAsFileTime returns the number of 100 nanosecond -// intervals since Jan 1, 1601 in a structure. Copy the high bits to -// the 64 bit tmpres, shift it left by 32 then or in the low 32 bits. - tmpres |= ft.dwHighDateTime; - tmpres <<= 32; - tmpres |= ft.dwLowDateTime; - -// Convert to microseconds by dividing by 10 - tmpres /= 10; - -// The Unix epoch starts on Jan 1 1970. Need to subtract the difference -// in seconds from Jan 1 1601. - tmpres -= DELTA_EPOCH_IN_MICROSECS; - -// Finally change microseconds to seconds and place in the seconds value. -// The modulus picks up the microseconds. - tv->tv_sec = (long)(tmpres / 1000000UL); - tv->tv_usec = (long)(tmpres % 1000000UL); - } - - if (NULL != tz) - { - if (!tzflag) - { - _tzset(); - tzflag++; - } - -// Adjust for the timezone west of Greenwich - tz->tz_minuteswest = _timezone / 60; - tz->tz_dsttime = _daylight; - } - - return 0; -} +class cpuTimer { + double start; + std::chrono::time_point<std::chrono::steady_clock> Start; -#else +public: + void reset() { + start=cpuTime(); + Start=std::chrono::steady_clock::now(); + } -#include <sys/time.h> + cpuTimer() { + reset(); + } -#endif + double nanoseconds() { + auto Stop=std::chrono::steady_clock::now(); + double stop=cpuTime(); -namespace utils { + return + std::min((double) std::chrono::duration_cast<std::chrono::nanoseconds> + (Stop-Start).count(),stop-start); + } -inline double totalseconds() -{ - timeval tv; - gettimeofday(&tv,NULL); - return tv.tv_sec+tv.tv_usec/1000000.0; -} - -inline double seconds() -{ - static double lastseconds=totalseconds(); - double t=totalseconds(); - double seconds=t-lastseconds; - lastseconds=t; - return seconds; -} + double seconds() { + return 1.0e-9*nanoseconds(); + } +}; } |