Path: csiph.com!weretis.net!feeder8.news.weretis.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 16:15:33 -0700 Organization: None to speak of Lines: 59 Message-ID: <87tu3el3ai.fsf@nosuchdomain.example.com> References: <8735ayml3k.fsf@nosuchdomain.example.com> MIME-Version: 1.0 Content-Type: text/plain Injection-Info: reader01.eternal-september.org; posting-host="4259117d4a6a84457329eab327d8f4cc"; logging-data="2185561"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/LlUGCHTPfheE3XjjBm+wI" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) Cancel-Lock: sha1:e29ghUY/UDkguVXF6/CcVLh1rws= sha1:OjTDX6NGf6jYbo5/YiYzSloUkl8= Xref: csiph.com comp.lang.c:168138 comp.lang.c++:87249 Lynn McGuire writes: > On 11/4/2022 5:05 PM, Keith Thompson wrote: >> Lynn McGuire writes: >>> On 11/4/2022 2:35 AM, Juha Nieminen wrote: >>>> In comp.lang.c++ Lynn McGuire wrote: >>>>> 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. >>> >>> 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 >> An alternative is to keep a counter, incremented for each generated >> file >> name, and append it to the file name, perhaps resetting to 0 when the >> seconds count advances. If your only requirement is uniqueness, that >> should be good enough. >> Digression: I remember an implementation of gettimeofday() that did >> something like that (on SunOS, maybe?). The system clock had a >> resolution of 0.01 second, so the value of tv_usec on successive calls >> might be: >> 420000 >> 420001 >> 420002 >> 430000 >> 430001 >> 430002 >> ... >> I don't remember whether the implicit counter was per-process or >> system-wide. > > The problem is multiple instances of the same program creating the > same name files. You could incorporate the process id into the file name. There's no truly portable way to do that, but one solution for POSIX and another for Windows is likely to be good enough. -- 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 */