- what is the best way to get microseconds of time on Windows in C or C++ ? - 16 Updates
- What is this header good for ? - 1 Update
- Why does string.replace not return an iterator? - 2 Updates
- why use static_cast ? - 3 Updates
- Simulating Halt Decider Applied to the Halting Theorem - 2 Updates
- "The pool of talented C++ developers is running dry" - 1 Update
Lynn McGuire <lynnmcguire5@gmail.com>: Nov 03 08:14PM -0500 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 |
Lynn McGuire <lynnmcguire5@gmail.com>: Nov 04 12:06AM -0500 On 11/3/2022 8:51 PM, DFS wrote: > 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. Thanks, Lynn McGuire |
Christian Gollwitzer <auriocus@gmx.de>: Nov 04 08:03AM +0100 HI Lynn, Am 04.11.22 um 06:06 schrieb Lynn McGuire: >> ---------------------------------------------------------------- > I forgot to add I would prefer a portable way if possible. I suspect > that is not possible though below the second resolution. I fear it is not possible in a portable way. Here you can find some description about how Tcl has solved this issue especially for Windows: https://core.tcl-lang.org/tips/doc/trunk/tip/7.md The implementation is here: https://github.com/tcltk/tcl/blob/main/win/tclWinTime.c and here for Unix: https://github.com/tcltk/tcl/blob/main/unix/tclUnixTime.c (BSD licensed, in any case) Christian |
Juha Nieminen <nospam@thanks.invalid>: Nov 04 07:35AM > What is the best way to get microseconds of time on Windows in C or C++ It's a bit unclear whether you mean "time in microseconds units" or "time at the microseconds resolution". Because the latter may not be even possible (portable or not). Just because some time function returns time "in microseconds" that doesn't mean that it uses microsecond resolution (in other words, the value it returns may well change at longer intervals than every microsecond; probably *significantly* longer intervals). It is possible to get some kind of timing at the CPU clock cycle interval (in most modern CPUs, at least), which is about as accurate as you can possibly get, but I don't know if there are eg. some system functions that will return elapsed clock cycles converted into the equivalent amount of microseconds. |
"Öö Tiib" <ootiib@hot.ee>: Nov 04 01:23AM -0700 On Friday, 4 November 2022 at 03:14:23 UTC+2, Lynn McGuire wrote: > integer imin = now -> tm_min; > integer isec = now -> tm_sec; > integer ihund = 0; I have such code snippet copy-pasted from somewhere about portable since C++11. auto t0 = std::chrono::high_resolution_clock::now(); auto nanosec = t0.time_since_epoch(); std::cout << nanosec.count() << " nanoseconds since epoch\n"; std::cout << nanosec.count() / (1000000000.0 * 60.0 * 60.0) << " hours since epoch\n"; As I rarely need nanoseconds haven't tried it everywhere but at least looks portable. |
Michael S <already5chosen@yahoo.com>: Nov 04 03:12AM -0700 On Friday, November 4, 2022 at 3:14:23 AM UTC+2, Lynn McGuire wrote: > integer ihund = 0; > Thanks, > Lynn Are you interested in absolute time or in measuring time intervals for profiling and similar purposes? For the former, usec resolution is obviously impossible, because crystals that drive time measurement in your typical PC are not very good and time adjustments via NTP are not particularly frequent. Not just microseconds, even whole second are typically no good. For the later, as long as measured intervals are relatively short (no more than 10-20 msec) usec resolution is physically possible. The most portable way is std::chrono::steady_clock. Less portable way (unreliable with Windows+gcc under mingw) but good with Windows+MSVC and Linux+gcc, is std::chrono::high_resolution_clock Another good way that is not portable in absolute sense, but very portable between various x86 OSes and environments is __rdtsc(). Except that I am not aware of portable way of figuring out a duration of the tick. Well, except of calibration followed by rounding to nearest 50MHz, but that method is non-satisfactory. In practice, quite often you just know the frequency. And even more often non-portable way of finding it is good enough. |
scott@slp53.sl.home (Scott Lurndal): Nov 04 01:44PM > integer ihund = 0; >Thanks, >Lynn First answer here: https://stackoverflow.com/questions/10905892/equivalent-of-gettimeday-for-windows |
Keith Thompson <Keith.S.Thompson+u@gmail.com>: Nov 04 09:07AM -0700 >> ---------------------------------------------------------------- > 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 */ |
Lynn McGuire <lynnmcguire5@gmail.com>: Nov 04 02:24PM -0500 On 11/4/2022 2:35 AM, Juha Nieminen wrote: > possibly get, but I don't know if there are eg. some system functions that > will return elapsed clock cycles converted into the equivalent amount of > microseconds. Get time at the microseconds resolution. My calculation engine creates a bunch of temporary files and I use the time to create unique names for those temporary files. Seconds should be good enough for uniqueness but I like adding microseconds as a guarantee. Something like uniquac1.out.009.20181972.20220912 Thanks, Lynn |
Lynn McGuire <lynnmcguire5@gmail.com>: Nov 04 02:32PM -0500 On 11/4/2022 2:03 AM, Christian Gollwitzer wrote: > https://github.com/tcltk/tcl/blob/main/unix/tclUnixTime.c > (BSD licensed, in any case) > Christian Thanks ! Lynn |
Lynn McGuire <lynnmcguire5@gmail.com>: Nov 04 02:33PM -0500 On 11/4/2022 11:07 AM, Keith Thompson wrote: > 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. Thanks ! Lynn |
Lynn McGuire <lynnmcguire5@gmail.com>: Nov 04 02:34PM -0500 On 11/4/2022 8:44 AM, Scott Lurndal wrote: >> Lynn > First answer here: > https://stackoverflow.com/questions/10905892/equivalent-of-gettimeday-for-windows Thanks ! Lynn |
Lynn McGuire <lynnmcguire5@gmail.com>: Nov 04 02:35PM -0500 On 11/4/2022 5:12 AM, Michael S wrote: > non-satisfactory. > In practice, quite often you just know the frequency. And > even more often non-portable way of finding it is good enough. Both measuring time intervals and the current time. Thanks ! Lynn |
Bonita Montero <Bonita.Montero@gmail.com>: Nov 04 08:49PM +0100 Am 04.11.2022 um 02:14 schrieb Lynn McGuire: > integer ihund = 0; > Thanks, > Lynn Use GetSystemTimeAsFileTime() and FileTimeToLocalFileTime() on Windows or the time_t-capable Functions on Unix and this class: Header: #pragma once #if defined(_MSC_VER) #define NOMINMAX #include <Windows.h>
Subscribe to:
Post Comments (Atom)
|
No comments:
Post a Comment