Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #168134

Re: what is the best way to get microseconds of time on Windows in C or C++ ?

Path csiph.com!news.mixmin.net!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From Keith Thompson <Keith.S.Thompson+u@gmail.com>
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 15:05:35 -0700
Organization None to speak of
Lines 50
Message-ID <8735ayml3k.fsf@nosuchdomain.example.com> (permalink)
References <tk1p0t$1iv03$1@dont-email.me> <tk2fch$13ej$1@gioia.aioe.org> <tk3oto$1va6e$1@dont-email.me>
MIME-Version 1.0
Content-Type text/plain
Injection-Info reader01.eternal-september.org; posting-host="d74099c2b109b60a9d4b4d88adbab987"; logging-data="2148850"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18NUhpmNlkn6CypYgcyqsGX"
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock sha1:1OVqhe9hBUb2LJMF4dfQ6ks6d5A= sha1:EC/bYIwRxHCwSBn0x1GWVzEJGYA=
Xref csiph.com comp.lang.c:168134 comp.lang.c++:87246

Cross-posted to 2 groups.

Show key headers only | View raw


Lynn McGuire <lynnmcguire5@gmail.com> writes:
> On 11/4/2022 2:35 AM, Juha Nieminen wrote:
>> In comp.lang.c++ Lynn McGuire <lynnmcguire5@gmail.com> 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.

-- 
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 */

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

what is the best way to get microseconds of time on Windows in C or C++ ? Lynn McGuire <lynnmcguire5@gmail.com> - 2022-11-03 20:14 -0500
  Re: what is the best way to get microseconds of time on Windows in C or C++ ? DFS <nospam@dfs.com> - 2022-11-03 21:51 -0400
    Re: what is the best way to get microseconds of time on Windows in C or C++ ? Lynn McGuire <lynnmcguire5@gmail.com> - 2022-11-04 00:06 -0500
      Re: what is the best way to get microseconds of time on Windows in C or C++ ? Christian Gollwitzer <auriocus@gmx.de> - 2022-11-04 08:03 +0100
        Re: what is the best way to get microseconds of time on Windows in C or C++ ? Lynn McGuire <lynnmcguire5@gmail.com> - 2022-11-04 14:32 -0500
      Re: what is the best way to get microseconds of time on Windows in C or C++ ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-11-04 09:07 -0700
        Re: what is the best way to get microseconds of time on Windows in C or C++ ? Lynn McGuire <lynnmcguire5@gmail.com> - 2022-11-04 14:33 -0500
      Re: what is the best way to get microseconds of time on Windows in C or C++ ? Opus <ifonly@youknew.org> - 2022-11-06 20:53 +0100
  Re: what is the best way to get microseconds of time on Windows in C or C++ ? Juha Nieminen <nospam@thanks.invalid> - 2022-11-04 07:35 +0000
    Re: what is the best way to get microseconds of time on Windows in C or C++ ? Lynn McGuire <lynnmcguire5@gmail.com> - 2022-11-04 14:24 -0500
      Re: what is the best way to get microseconds of time on Windows in C or C++ ? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-11-04 19:50 +0000
        Re: what is the best way to get microseconds of time on Windows in C or C++ ? Lynn McGuire <lynnmcguire5@gmail.com> - 2022-11-04 15:16 -0500
      Re: what is the best way to get microseconds of time on Windows in C or C++ ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-11-04 15:05 -0700
        Re: what is the best way to get microseconds of time on Windows in C or C++ ? Lynn McGuire <lynnmcguire5@gmail.com> - 2022-11-04 17:35 -0500
          Re: what is the best way to get microseconds of time on Windows in C or C++ ? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2022-11-04 15:56 -0700
          Re: what is the best way to get microseconds of time on Windows in C or C++ ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-11-04 16:15 -0700
  Re: what is the best way to get microseconds of time on Windows in C or C++ ? scott@slp53.sl.home (Scott Lurndal) - 2022-11-04 13:44 +0000
    Re: what is the best way to get microseconds of time on Windows in C or C++ ? Lynn McGuire <lynnmcguire5@gmail.com> - 2022-11-04 14:34 -0500
    Re: what is the best way to get microseconds of time on Windows in C or C++ ? Lynn McGuire <lynnmcguire5@gmail.com> - 2022-11-14 16:58 -0600
      Re: what is the best way to get microseconds of time on Windows in C or C++ ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-11-14 17:36 -0800
        Re: what is the best way to get microseconds of time on Windows in C or C++ ? Lynn McGuire <lynnmcguire5@gmail.com> - 2022-11-15 17:26 -0600
          Re: what is the best way to get microseconds of time on Windows in C or C++ ? Andreas <nospam@gmx.de> - 2022-11-16 13:15 +0100
  Re: what is the best way to get microseconds of time on Windows in C or C++ ? Bonita Montero <Bonita.Montero@gmail.com> - 2022-11-04 20:49 +0100
    Re: what is the best way to get microseconds of time on Windows in C or C++ ? Bart <bc@freeuk.com> - 2022-11-04 22:31 +0000
      Re: what is the best way to get microseconds of time on Windows in C or C++ ? Bonita Montero <Bonita.Montero@gmail.com> - 2022-11-05 06:40 +0100
  Re: what is the best way to get microseconds of time on Windows in C or C++ ? Manu Raju <MR@invalid.invalid> - 2022-11-20 21:30 +0000

csiph-web