Path: csiph.com!news.mixmin.net!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c,comp.lang.c++ Subject: Re: what is the best way to get microseconds of time on Windows in C or C++ ? Date: Fri, 04 Nov 2022 09:07:58 -0700 Organization: None to speak of Lines: 79 Message-ID: <87bkpmn1nl.fsf@nosuchdomain.example.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: reader01.eternal-september.org; posting-host="d74099c2b109b60a9d4b4d88adbab987"; logging-data="1979006"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/jkdP4zODLdhFvZnsjOC2S" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) Cancel-Lock: sha1:hPmpDjHqZ53ce5XkqbSBc1D8QWg= sha1:ipmpL2+NdodUUUBo/Ff4lN63418= Xref: csiph.com comp.lang.c:168126 comp.lang.c++:87232 Lynn McGuire writes: > On 11/3/2022 8:51 PM, DFS wrote: >> On 11/3/2022 9:14 PM, Lynn McGuire wrote: >>> What is the best way to get microseconds of time on Windows in C or >>> C++ ?  I am currently using the following code to get the current >>> time: >>> >>>      std::time_t t = std::time (0);   // get time now >>>      std::tm * now = std::localtime ( & t); >>>      integer iyear = now -> tm_year + 1900; >>>      integer imonth = now -> tm_mon + 1; >>>      integer iday = now -> tm_mday; >>>      integer ihour = now -> tm_hour; >>>      integer imin = now -> tm_min; >>>      integer isec = now -> tm_sec; >>>      integer ihund = 0; >>> >>> Thanks, >>> Lynn >> >> https://learn.microsoft.com/en-us/windows/win32/sysinfo/acquiring-high-resolution-time-stamps >> >> I used it like this: >> ---------------------------------------------------------------- >> LARGE_INTEGER frequency,start,end; >> //calc elapsed time >> double elapsedtime(LARGE_INTEGER startingtimer) >> { >>   QueryPerformanceCounter(&end); >>   return (end.QuadPart - startingtimer.QuadPart) / >> (double)frequency.QuadPart; >> } >> >> int main(void) >> { >>     //turn on Windows program timing >>     QueryPerformanceFrequency(&frequency); >>     //start timing a piece of code >>     QueryPerformanceCounter(&start); >>     ... >>     ... >>     printf(" %.4fs to execute\n",elapsedtime(start)); >> } >> ---------------------------------------------------------------- > > I forgot to add I would prefer a portable way if possible. I suspect > that is not possible though below the second resolution. The resolution of the time() function is unspecified (not even implementation-defined). In practice, time_t is almost universally an integer type representing seconds since some epoch, usually 1970-01-01 00:00:00 UTC, but it could even be floating-point. C11 adds the type struct timespec and the function timespec_get() which gives a result with a resolution of 1 nanosecond. A footnote in the standard says: Although a struct timespec object describes times with nanosecond resolution, the available resolution is system dependent and may even be greater than 1 second. The standard says that struct timespec has the following members: time_t tv_sec; // whole seconds — ≥ 0 long tv_nsec; // nanoseconds — [0, 999999999] which strongly implies that the resulution of time_t is 1 second, but the standard still doesn't say so. Possibly an implementation could use different encodings for the time_t value from time() and the time_t value from timespec_get(), though that's unlikely in practice. clock() returns a value of type clock_t, which is typically double, but that's also unspecified -- and it measures processor time, not wall clock time. -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com Working, but not speaking, for XCOM Labs void Void(void) { Void(); } /* The recursive call of the void */