Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Machines that have broken down will work perfectly when the repairman arrives.


devel / comp.lang.c / what is the best way to get microseconds of time on Windows in C or C++ ?

SubjectAuthor
* what is the best way to get microseconds of time on Windows in C orLynn McGuire
+* Re: what is the best way to get microseconds of time on Windows in CDFS
|`* Re: what is the best way to get microseconds of time on Windows in CLynn McGuire
| +* Re: what is the best way to get microseconds of time on Windows in CChristian Gollwitzer
| |`- Re: what is the best way to get microseconds of time on Windows in CLynn McGuire
| +* Re: what is the best way to get microseconds of time on Windows in C or C++ ?Keith Thompson
| |`- Re: what is the best way to get microseconds of time on Windows in CLynn McGuire
| `- Re: what is the best way to get microseconds of time on Windows in COpus
+* Re: what is the best way to get microseconds of time on Windows in C or C++ ?Juha Nieminen
|`* Re: what is the best way to get microseconds of time on Windows in CLynn McGuire
| +* Re: what is the best way to get microseconds of time on Windows in C or C++ ?Ben Bacarisse
| |`- Re: what is the best way to get microseconds of time on Windows in CLynn McGuire
| `* Re: what is the best way to get microseconds of time on Windows in C or C++ ?Keith Thompson
|  `* Re: what is the best way to get microseconds of time on Windows in CLynn McGuire
|   +- Re: what is the best way to get microseconds of time on Windows in CChris M. Thomasson
|   `- Re: what is the best way to get microseconds of time on Windows in C or C++ ?Keith Thompson
+* Re: what is the best way to get microseconds of time on Windows in C or C++ ?Scott Lurndal
|+- Re: what is the best way to get microseconds of time on Windows in CLynn McGuire
|`* Re: what is the best way to get microseconds of time on Windows in CLynn McGuire
| `* Re: what is the best way to get microseconds of time on Windows in C or C++ ?Keith Thompson
|  `* Re: what is the best way to get microseconds of time on Windows in CLynn McGuire
|   `- Re: what is the best way to get microseconds of time on Windows in CAndreas
+* Re: what is the best way to get microseconds of time on Windows in CBonita Montero
|`* Re: what is the best way to get microseconds of time on Windows in CBart
| `- Re: what is the best way to get microseconds of time on Windows in CBonita Montero
`- Re: what is the best way to get microseconds of time on Windows in CManu Raju

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

<tk1p0t$1iv03$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23582&group=comp.lang.c#23582

  copy link   Newsgroups: comp.lang.c comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: lynnmcgu...@gmail.com (Lynn McGuire)
Newsgroups: comp.lang.c,comp.lang.c++
Subject: what is the best way to get microseconds of time on Windows in C or
C++ ?
Date: Thu, 3 Nov 2022 20:14:05 -0500
Organization: A noiseless patient Spider
Lines: 15
Message-ID: <tk1p0t$1iv03$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 4 Nov 2022 01:14:05 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="657e75a71a2bfc0b3f9ae0d2447ebf27";
logging-data="1670147"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+BeC/7PF1oPW+xYt6h+SF3"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.4.1
Cancel-Lock: sha1:Cf2mF40BKhsRF4pDMErMamCIMQ0=
Content-Language: en-US
 by: Lynn McGuire - Fri, 4 Nov 2022 01:14 UTC

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

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

<Je_8L.9313$Iwb3.8230@fx13.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23583&group=comp.lang.c#23583

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx13.iad.POSTED!not-for-mail
MIME-Version: 1.0
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.13.1
Subject: Re: what is the best way to get microseconds of time on Windows in C
or C++ ?
Content-Language: en-US
Newsgroups: comp.lang.c
References: <tk1p0t$1iv03$1@dont-email.me>
From: nos...@dfs.com (DFS)
In-Reply-To: <tk1p0t$1iv03$1@dont-email.me>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Lines: 48
Message-ID: <Je_8L.9313$Iwb3.8230@fx13.iad>
X-Complaints-To: abuse@blocknews.net
NNTP-Posting-Date: Fri, 04 Nov 2022 01:51:37 UTC
Organization: blocknews - www.blocknews.net
Date: Thu, 3 Nov 2022 21:51:38 -0400
X-Received-Bytes: 2139
 by: DFS - Fri, 4 Nov 2022 01:51 UTC

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));

}
----------------------------------------------------------------

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

<tk26lf$1moc1$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23584&group=comp.lang.c#23584

  copy link   Newsgroups: comp.lang.c comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: lynnmcgu...@gmail.com (Lynn McGuire)
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, 4 Nov 2022 00:06:54 -0500
Organization: A noiseless patient Spider
Lines: 55
Message-ID: <tk26lf$1moc1$1@dont-email.me>
References: <tk1p0t$1iv03$1@dont-email.me> <Je_8L.9313$Iwb3.8230@fx13.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 4 Nov 2022 05:06:55 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="657e75a71a2bfc0b3f9ae0d2447ebf27";
logging-data="1794433"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18ImuY/Oun5DrqB4SP7nZl0"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.4.1
Cancel-Lock: sha1:ANCa+C0oyvUNJiFPnievSJKFO0o=
In-Reply-To: <Je_8L.9313$Iwb3.8230@fx13.iad>
Content-Language: en-US
 by: Lynn McGuire - Fri, 4 Nov 2022 05:06 UTC

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.

Thanks,
Lynn McGuire

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

<tk2dfa$1n773$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23585&group=comp.lang.c#23585

  copy link   Newsgroups: comp.lang.c comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: aurio...@gmx.de (Christian Gollwitzer)
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, 4 Nov 2022 08:03:06 +0100
Organization: A noiseless patient Spider
Lines: 31
Message-ID: <tk2dfa$1n773$1@dont-email.me>
References: <tk1p0t$1iv03$1@dont-email.me> <Je_8L.9313$Iwb3.8230@fx13.iad>
<tk26lf$1moc1$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 4 Nov 2022 07:03:06 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="fe2abef03d24811e58a3e3b4832b486c";
logging-data="1809635"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19VfRC9I9lj4fB75ogrVkKvrc3qWlwb30A="
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:102.0)
Gecko/20100101 Thunderbird/102.4.0
Cancel-Lock: sha1:EPc7EvtbcKyq55g9WDHmKyBweUg=
In-Reply-To: <tk26lf$1moc1$1@dont-email.me>
 by: Christian Gollwitzer - Fri, 4 Nov 2022 07:03 UTC

HI Lynn,
Am 04.11.22 um 06:06 schrieb Lynn McGuire:
> 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:
>>>

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

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

<tk2fch$13ej$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23586&group=comp.lang.c#23586

  copy link   Newsgroups: comp.lang.c comp.lang.c++
Path: i2pn2.org!i2pn.org!aioe.org!NK0c7qMEn6mmBWqphs27pg.user.46.165.242.75.POSTED!not-for-mail
From: nos...@thanks.invalid (Juha Nieminen)
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, 4 Nov 2022 07:35:47 -0000 (UTC)
Organization: Aioe.org NNTP Server
Message-ID: <tk2fch$13ej$1@gioia.aioe.org>
References: <tk1p0t$1iv03$1@dont-email.me>
Injection-Info: gioia.aioe.org; logging-data="36307"; posting-host="NK0c7qMEn6mmBWqphs27pg.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: tin/2.4.5-20201224 ("Glen Albyn") (Linux/5.15.69-grsec-kapsi (x86_64))
X-Notice: Filtered by postfilter v. 0.9.2
 by: Juha Nieminen - Fri, 4 Nov 2022 07:35 UTC

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.

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

<KG89L.4244$KkB2.2249@fx47.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23587&group=comp.lang.c#23587

  copy link   Newsgroups: comp.lang.c comp.lang.c++
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx47.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: sco...@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: what is the best way to get microseconds of time on Windows in C or C++ ?
Newsgroups: comp.lang.c,comp.lang.c++
References: <tk1p0t$1iv03$1@dont-email.me>
Lines: 20
Message-ID: <KG89L.4244$KkB2.2249@fx47.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Fri, 04 Nov 2022 13:44:10 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Fri, 04 Nov 2022 13:44:10 GMT
X-Received-Bytes: 1270
 by: Scott Lurndal - Fri, 4 Nov 2022 13:44 UTC

Lynn McGuire <lynnmcguire5@gmail.com> writes:
>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

First answer here:

https://stackoverflow.com/questions/10905892/equivalent-of-gettimeday-for-windows

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

<87bkpmn1nl.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23588&group=comp.lang.c#23588

  copy link   Newsgroups: comp.lang.c comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (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: <tk1p0t$1iv03$1@dont-email.me> <Je_8L.9313$Iwb3.8230@fx13.iad>
<tk26lf$1moc1$1@dont-email.me>
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=
 by: Keith Thompson - Fri, 4 Nov 2022 16:07 UTC

Lynn McGuire <lynnmcguire5@gmail.com> 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 */

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

<tk3oto$1va6e$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23589&group=comp.lang.c#23589

  copy link   Newsgroups: comp.lang.c comp.lang.c++
Path: i2pn2.org!i2pn.org!aioe.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: lynnmcgu...@gmail.com (Lynn McGuire)
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, 4 Nov 2022 14:24:38 -0500
Organization: A noiseless patient Spider
Lines: 28
Message-ID: <tk3oto$1va6e$1@dont-email.me>
References: <tk1p0t$1iv03$1@dont-email.me> <tk2fch$13ej$1@gioia.aioe.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 4 Nov 2022 19:24:40 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="657e75a71a2bfc0b3f9ae0d2447ebf27";
logging-data="2074830"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/4W9z/iMPOgQ3vz9e/eQnQ"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.4.1
Cancel-Lock: sha1:B8n2qyAszhr40x6JOSqn1I82kys=
Content-Language: en-US
In-Reply-To: <tk2fch$13ej$1@gioia.aioe.org>
 by: Lynn McGuire - Fri, 4 Nov 2022 19:24 UTC

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

Thanks,
Lynn

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

<tk3pcn$1va6e$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23590&group=comp.lang.c#23590

  copy link   Newsgroups: comp.lang.c comp.lang.c++
Path: i2pn2.org!i2pn.org!aioe.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: lynnmcgu...@gmail.com (Lynn McGuire)
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, 4 Nov 2022 14:32:37 -0500
Organization: A noiseless patient Spider
Lines: 36
Message-ID: <tk3pcn$1va6e$2@dont-email.me>
References: <tk1p0t$1iv03$1@dont-email.me> <Je_8L.9313$Iwb3.8230@fx13.iad>
<tk26lf$1moc1$1@dont-email.me> <tk2dfa$1n773$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 4 Nov 2022 19:32:39 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="657e75a71a2bfc0b3f9ae0d2447ebf27";
logging-data="2074830"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19JmrbBYP0JMIUMIAPsQTPA"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.4.1
Cancel-Lock: sha1:elz6AaItN1P6Iqs+Q2iF2qrgSU0=
In-Reply-To: <tk2dfa$1n773$1@dont-email.me>
Content-Language: en-US
 by: Lynn McGuire - Fri, 4 Nov 2022 19:32 UTC

On 11/4/2022 2:03 AM, Christian Gollwitzer wrote:
> HI Lynn,
> Am 04.11.22 um 06:06 schrieb Lynn McGuire:
>> 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:
>>>>
>
>>> ----------------------------------------------------------------
>>
>> 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

Thanks !

Lynn

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

<tk3pei$1va6e$3@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23591&group=comp.lang.c#23591

  copy link   Newsgroups: comp.lang.c comp.lang.c++
Path: i2pn2.org!i2pn.org!aioe.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: lynnmcgu...@gmail.com (Lynn McGuire)
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, 4 Nov 2022 14:33:36 -0500
Organization: A noiseless patient Spider
Lines: 81
Message-ID: <tk3pei$1va6e$3@dont-email.me>
References: <tk1p0t$1iv03$1@dont-email.me> <Je_8L.9313$Iwb3.8230@fx13.iad>
<tk26lf$1moc1$1@dont-email.me> <87bkpmn1nl.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 4 Nov 2022 19:33:38 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="657e75a71a2bfc0b3f9ae0d2447ebf27";
logging-data="2074830"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Q+3p3iZtkW88p/RMTsjUA"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.4.1
Cancel-Lock: sha1:qbrO7kpEC9KdlRBskCQzx2f9J9U=
In-Reply-To: <87bkpmn1nl.fsf@nosuchdomain.example.com>
Content-Language: en-US
 by: Lynn McGuire - Fri, 4 Nov 2022 19:33 UTC

On 11/4/2022 11:07 AM, Keith Thompson wrote:
> Lynn McGuire <lynnmcguire5@gmail.com> 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.

Thanks !

Lynn

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

<tk3pfh$1va6e$4@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23592&group=comp.lang.c#23592

  copy link   Newsgroups: comp.lang.c comp.lang.c++
Path: i2pn2.org!i2pn.org!aioe.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: lynnmcgu...@gmail.com (Lynn McGuire)
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, 4 Nov 2022 14:34:08 -0500
Organization: A noiseless patient Spider
Lines: 26
Message-ID: <tk3pfh$1va6e$4@dont-email.me>
References: <tk1p0t$1iv03$1@dont-email.me> <KG89L.4244$KkB2.2249@fx47.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 4 Nov 2022 19:34:10 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="657e75a71a2bfc0b3f9ae0d2447ebf27";
logging-data="2074830"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/rA7TZKOk6scBMtQF7f2tv"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.4.1
Cancel-Lock: sha1:+gBz7w/kyjxWxv4GamBrg2VZjS8=
Content-Language: en-US
In-Reply-To: <KG89L.4244$KkB2.2249@fx47.iad>
 by: Lynn McGuire - Fri, 4 Nov 2022 19:34 UTC

On 11/4/2022 8:44 AM, Scott Lurndal wrote:
> Lynn McGuire <lynnmcguire5@gmail.com> writes:
>> 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
>
> First answer here:
>
> https://stackoverflow.com/questions/10905892/equivalent-of-gettimeday-for-windows

Thanks !

Lynn

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

<tk3qci$112r$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23593&group=comp.lang.c#23593

  copy link   Newsgroups: comp.lang.c comp.lang.c++
Path: i2pn2.org!i2pn.org!aioe.org!CIVizCVKCGKLM7+Tm+7XOQ.user.46.165.242.75.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
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, 4 Nov 2022 20:49:52 +0100
Organization: Aioe.org NNTP Server
Message-ID: <tk3qci$112r$1@gioia.aioe.org>
References: <tk1p0t$1iv03$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="33883"; posting-host="CIVizCVKCGKLM7+Tm+7XOQ.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.4.1
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: de-DE
 by: Bonita Montero - Fri, 4 Nov 2022 19:49 UTC

Am 04.11.2022 um 02:14 schrieb Lynn McGuire:
> 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

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>
#endif
#include <cstdint>
#include <compare>
#include <ctime>
#include <stdexcept>
#include <compare>
#include <memory>
#include <mutex>
#include <algorithm>
#include <vector>
#include <functional>
#include <atomic>

#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable: 26812) // prefer enum class over enum
#pragma warning(disable: 26495) // members not intitialized
#endif

struct st_timestamp
{ constexpr st_timestamp() = default;
st_timestamp( st_timestamp const & ) = default;
constexpr st_timestamp( std::uint64_t timestamp ) noexcept;
constexpr operator std::uint64_t() const noexcept;
constexpr explicit operator std::int64_t() const noexcept;
constexpr explicit operator bool() const noexcept;
constexpr st_timestamp &operator =( st_timestamp const & ) = default;
constexpr st_timestamp &operator =( std::uint64_t timestamp ) noexcept;
private:
std::uint64_t m_timestamp;
};

struct system_time
{ std::uint16_t year;
std::uint8_t weekday, month, day;
std::uint8_t hour, minute, second;
std::uint32_t ns100;
enum reason_t : uint8_t
{
TIMESTAMP_BEYOND_63_BIT = 1,
YEAR_OUT_OF_RANGE,
MONTH_OUT_OF_RANGE,
DAY_OUT_OF_RANGE,
HOUR_OUT_OF_RANGE,
MINUTE_OUT_OF_RANGE,
SECOND_OUT_OF_RANGE,
NS100_OUT_OF_RANGE,
TIMESTAMP_BEFORE_TIME_T,
WRONG_LEAP_SECOND
};
struct leap_second_t
{
std::uint16_t year;
bool jun30, dec31;
};
struct date_error : public std::invalid_argument
{
reason_t reason();
date_error() = delete;
date_error( date_error const & ) = delete;
private:
friend struct system_time;
date_error( reason_t reason );
reason_t m_reason;
};
system_time() {}
system_time( system_time const & ) = default;
system_time( std::uint16_t year, std::uint8_t month, std::uint8_t day,
std::uint8_t hour, std::uint8_t minute, std::uint8_t second,
std::uint32_t ns100 );
explicit system_time( st_timestamp ts );
system_time( struct tm const &tm );
#if defined(_MSC_VER)
system_time( SYSTEMTIME const &st );
#endif
operator st_timestamp() const;
st_timestamp to_timestamp( reason_t *reason = nullptr ) const noexcept;
system_time &operator =( st_timestamp ts );
void from_timestamp( st_timestamp ts, reason_t *reason = nullptr )
noexcept;
system_time &operator =( system_time const & ) = default;
operator tm() const;
struct tm to_tm( reason_t *reason ) const noexcept;
system_time &operator =( struct tm const &tm );
bool from_tm( struct tm const &tm, reason_t *reason ) noexcept;
#if defined(_MSC_VER)
operator SYSTEMTIME() const;
SYSTEMTIME to_systemtime( reason_t *reason ) const noexcept;
system_time &operator =( SYSTEMTIME const &st );
bool from_systemtime( SYSTEMTIME const &st, reason_t *reason ) noexcept;
#endif
bool adjust_weekday( reason_t *reason = nullptr ) noexcept;
static std::uint8_t get_weekday( st_timestamp timestamp ) noexcept;
static_assert(sizeof(time_t) == 8, "time_t must be 64 bit");
static time_t timestamp_to_time_t( st_timestamp ts, reason_t *reason =
nullptr ) noexcept;
static st_timestamp time_t_to_timestamp( time_t time, reason_t *reason
= nullptr ) noexcept;
static void leap_seconds( std::function<leap_second_t ( bool )> const
&fn );
private:
friend struct st_timestamp;
static std::atomic<std::shared_ptr<std::vector<leap_second_t>>>
g_leapSeconds;
static constexpr std::uint64_t
MILLISECOND = 10'000,
SECOND = 1'000 * MILLISECOND,
MINUTE = 60 * SECOND,
HOUR = 60 * MINUTE,
DAY = 24 * HOUR,
WEEK = 7 * DAY,
NON_LEAP_YEAR = 365 * DAY,
LEAP_YEAR = 366 * DAY,
FOUR_YEARS_W_LJ = LEAP_YEAR + 3 * NON_LEAP_YEAR,
FOUR_YEARS_WO_LJ = 4 * NON_LEAP_YEAR,
FIRST_QUARTER_CENTURY = 25 * FOUR_YEARS_W_LJ,
REMAINING_QUARTER_CENUTRIES = 25 * FOUR_YEARS_W_LJ - DAY,
FOUR_HUNDRED_YEARS = FIRST_QUARTER_CENTURY + 3 *
REMAINING_QUARTER_CENUTRIES,
TIME_T_IN_TIMESTAMP_BEGIN = 0x19DB1DED53E800,
LAST_TIMESTAMP_IN_TIME_T = 0xD69433CCD5,
LEAP_SECOND_BIT = std::numeric_limits<int64_t>::min();
};

constexpr st_timestamp::st_timestamp( std::uint64_t timestamp ) noexcept :
m_timestamp( timestamp )
{ }

constexpr st_timestamp::operator std::uint64_t() const noexcept
{ return m_timestamp & ~system_time::LEAP_SECOND_BIT;
}

constexpr st_timestamp::operator std::int64_t() const noexcept
{ return m_timestamp;
}

constexpr st_timestamp::operator bool() const noexcept
{ return m_timestamp & system_time::LEAP_SECOND_BIT;
}

constexpr st_timestamp &st_timestamp::operator =( std::uint64_t
timestamp ) noexcept
{ m_timestamp = timestamp;
return *this;
}

// may throw invalid_argument

inline system_time::system_time( st_timestamp ts )
{ *this = ts;
}

// may throw invalid_argument

inline system_time &system_time::operator =( st_timestamp ts )
{ reason_t reason;
from_timestamp( ts, &reason );
if( reason ) [[likely]]
throw date_error( reason );
return *this;
}

inline system_time::system_time( struct tm const &tm )
{ *this = tm;
}

#if defined(_MSC_VER)
inline system_time::system_time( SYSTEMTIME const &st )
{ *this = st;
} #endif

inline typename system_time::reason_t system_time::date_error::reason()
{ return m_reason;
}

inline uint8_t system_time::get_weekday( st_timestamp ts ) noexcept
{ using namespace std;
return (uint32_t)(((uint64_t)ts - (uint64_t)ts / WEEK * WEEK) / DAY +
1) % 7;
}

// may throw date_error

inline std::strong_ordering operator <=>( system_time const &left,
system_time const &right )
{ return (std::uint64_t)(st_timestamp)left <=> (uint64_t)(st_timestamp)right;
}

// may throw date_error

inline std::strong_ordering operator <=>( system_time const &left,
st_timestamp right )
{ return (std::uint64_t)(st_timestamp)left <=> (std::uint64_t)right;
}

// may throw date_error

inline std::strong_ordering operator <=>( st_timestamp left, system_time
const &right )
{ return (std::uint64_t)left <=> (std::uint64_t)(st_timestamp)left;
}

#if defined(_MSC_VER)
#pragma warning(pop)
#endif

// .cpp:

#include <stdexcept>
#include <cassert>
#include <limits>
#include <algorithm>
#include <array>
#include <system_error>
#include <functional>
#if defined(__cpp_lib_hardware_interference_size)
#include <new>
#endif
#include "system_time.h"

using namespace std;

#if defined(__cpp_lib_hardware_interference_size)
constexpr size_t CL_SIZE = std::hardware_destructive_interference_size;
#else
constexpr size_t CL_SIZE = 64;
#endif

constexpr bool SYSTEM_TIME_BINARY_SEARCH = false;

// may throw date_error

system_time::system_time( std::uint16_t year, std::uint8_t month,
std::uint8_t day, std::uint8_t hour, std::uint8_t minute, std::uint8_t
second, std::uint32_t ns100 )
{ this->year = year;
this->month = month;
this->day = day;
this->hour = hour;
this->minute = minute;
this->second = second;
this->ns100 = ns100;
this->weekday = get_weekday( (st_timestamp)*this );
}

// may throw date_error

void system_time::from_timestamp( st_timestamp ts, reason_t *reason )
noexcept
{ auto reasonableErr = [&]( reason_t retReason ) -> void
{
if( reason ) [[likely]]
*reason = retReason;
memset( this, 0, sizeof *this );
};
uint64_t tsCalc = (uint64_t)ts;
bool leapSecond = (bool)ts;
weekday = get_weekday( ts );
tsCalc += LEAP_YEAR; // 1601 - 1600 = leap year
uint16_t y400, y100, y4, y;
y400 = (uint16_t)(tsCalc / FOUR_HUNDRED_YEARS);
tsCalc -= y400 * FOUR_HUNDRED_YEARS;
bool isLeapYear;
auto leapQuad = [&]()
{
if( tsCalc >= LEAP_YEAR ) [[likely]]
// y >= 1
tsCalc -= LEAP_YEAR,
y = (uint16_t)(1 + tsCalc / NON_LEAP_YEAR), // 1 ... 3
tsCalc -= tsCalc / NON_LEAP_YEAR * NON_LEAP_YEAR,
isLeapYear = false;
else
// y == 0
y = 0,
isLeapYear = true;
};
if( tsCalc >= FIRST_QUARTER_CENTURY ) [[likely]]
{
// (y % 400) >= 100
y100 = (uint16_t)(1 + (tsCalc - FIRST_QUARTER_CENTURY) /
REMAINING_QUARTER_CENUTRIES); // 1 ... 3
tsCalc -= FIRST_QUARTER_CENTURY + (tsCalc - FIRST_QUARTER_CENTURY) /
REMAINING_QUARTER_CENUTRIES * REMAINING_QUARTER_CENUTRIES;
if( tsCalc >= FOUR_YEARS_WO_LJ ) [[likely]]
// (y % 400) >= 100 && (y % 100) >= 4
y4 = (uint16_t)(1 + (tsCalc - FOUR_YEARS_WO_LJ) / FOUR_YEARS_W_LJ),
// 1 ... 24
tsCalc -= FOUR_YEARS_WO_LJ + (tsCalc - FOUR_YEARS_WO_LJ) /
FOUR_YEARS_W_LJ * FOUR_YEARS_W_LJ,
leapQuad();
else
// (y % 400) >= 100 && (y % 100) < 4
y4 = 0,
y = (uint16_t)(tsCalc / NON_LEAP_YEAR),
tsCalc -= tsCalc / NON_LEAP_YEAR * NON_LEAP_YEAR,
isLeapYear = false;
}
else
// (y % 400) < 100
y100 = 0,
y4 = (uint16_t)(tsCalc / FOUR_YEARS_W_LJ),
tsCalc -= tsCalc / FOUR_YEARS_W_LJ * FOUR_YEARS_W_LJ,
leapQuad();
year = 1600 + 400 * y400 + 100 * y100 + 4 * y4 + y;
{
static uint64_t const monthOffsets[2][12 + 1] alignas(CL_SIZE) =
{
{ 0 * DAY, 31 * DAY, 59 * DAY, 90 * DAY, 120 * DAY, 151 * DAY, 181 *
DAY, 212 * DAY, 243 * DAY, 273 * DAY, 304 * DAY, 334 * DAY, 999 * DAY },
{ 0 * DAY, 31 * DAY, 60 * DAY, 91 * DAY, 121 * DAY, 152 * DAY, 182 *
DAY, 213 * DAY, 244 * DAY, 274 * DAY, 305 * DAY, 335 * DAY, 999 * DAY }
};
uint64_t const *pMonthOffsets = monthOffsets[isLeapYear];
size_t moHit;
if constexpr( !SYSTEM_TIME_BINARY_SEARCH )
for( moHit = 0; tsCalc >= pMonthOffsets[moHit + 1]; ++moHit );
else
{
size_t lower = 0, upper = 12, mid;
moHit = -1;
do
{
mid = (lower + upper) / 2;
if( pMonthOffsets[mid] <= tsCalc )
moHit = mid,
lower = mid + 1;
else
upper = mid;
} while( lower != upper );
assert( moHit != -1 );
}
uint64_t mo = pMonthOffsets[moHit];
uint8_t dy = (uint8_t)((tsCalc - mo) / DAY);
tsCalc -= mo + dy * DAY;
month = 1 + (uint8_t)moHit;
day = 1 + dy;
}
hour = (uint8_t)(tsCalc / HOUR);
tsCalc %= HOUR;
minute = (uint8_t)(tsCalc / MINUTE);
tsCalc %= MINUTE;
second = (uint8_t)(tsCalc / SECOND);
if( leapSecond ) [[unlikely]]
{
if( second != 59 || minute != 59 || hour != 23 || (month != 6 || day
!= 30) && (month != 12 || day != 31) ) [[unlikely]]
return reasonableErr( WRONG_LEAP_SECOND );
shared_ptr leapSeconds( g_leapSeconds.load( memory_order_relaxed ) );
size_t lower = 0, upper = leapSeconds->size();
for( ; ; )
{
size_t mid = (lower + upper) / 2;
leap_second_t const &ls = (*leapSeconds)[mid];
if( ls.year == year ) [[unlikely]]
if( month == 6 && ls.jun30 || month == 12 && ls.dec31 ) [[likely]]
{
++second;
break;
}
else
return reasonableErr( WRONG_LEAP_SECOND );
bool midIsLower = ls.year < year;
(midIsLower ? lower : upper) = mid + midIsLower;
if( lower == upper ) [[unlikely]]
return reasonableErr( WRONG_LEAP_SECOND );
}
}
tsCalc %= SECOND;
ns100 = (uint32_t)tsCalc;
}


Click here to read the complete article
Re: what is the best way to get microseconds of time on Windows in C or C++ ?

<87pme233e6.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23594&group=comp.lang.c#23594

  copy link   Newsgroups: comp.lang.c comp.lang.c++
Path: i2pn2.org!i2pn.org!aioe.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
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 19:50:41 +0000
Organization: A noiseless patient Spider
Lines: 29
Message-ID: <87pme233e6.fsf@bsb.me.uk>
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="0941d159f338e786091fe4b719d2355d";
logging-data="2089486"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Lb2rIUYh7QMwGZpy/vu0v6TodDQ+Gs3M="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:3Tf/3+5rh67Osw3cu7fxPiR+kbY=
sha1:nAgOzvyXbkm5CJgGrEwEzwQPa0I=
X-BSB-Auth: 1.d9c28a4ca0177a2a0104.20221104195041GMT.87pme233e6.fsf@bsb.me.uk
 by: Ben Bacarisse - Fri, 4 Nov 2022 19:50 UTC

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

Have you ruled out tmpnam and tmpfile?

--
Ben.

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

<tk3rua$205ng$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23595&group=comp.lang.c#23595

  copy link   Newsgroups: comp.lang.c comp.lang.c++
Path: i2pn2.org!i2pn.org!aioe.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: lynnmcgu...@gmail.com (Lynn McGuire)
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, 4 Nov 2022 15:16:08 -0500
Organization: A noiseless patient Spider
Lines: 33
Message-ID: <tk3rua$205ng$1@dont-email.me>
References: <tk1p0t$1iv03$1@dont-email.me> <tk2fch$13ej$1@gioia.aioe.org>
<tk3oto$1va6e$1@dont-email.me> <87pme233e6.fsf@bsb.me.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 4 Nov 2022 20:16:10 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="657e75a71a2bfc0b3f9ae0d2447ebf27";
logging-data="2103024"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19nG2reLePnd7UYvsth5Rp+"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.4.1
Cancel-Lock: sha1:jU7HBBwBRt9XTvHn2UGESOkOM1o=
In-Reply-To: <87pme233e6.fsf@bsb.me.uk>
Content-Language: en-US
 by: Lynn McGuire - Fri, 4 Nov 2022 20:16 UTC

On 11/4/2022 2:50 PM, Ben Bacarisse wrote:
> 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
>
> Have you ruled out tmpnam and tmpfile?

I just ported this code from F77 to C++. I totally forgot about tmpnam
and tmpfile.

Thanks,
Lynn

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

<8735ayml3k.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23596&group=comp.lang.c#23596

  copy link   Newsgroups: comp.lang.c comp.lang.c++
Path: i2pn2.org!i2pn.org!aioe.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (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 15:05:35 -0700
Organization: None to speak of
Lines: 50
Message-ID: <8735ayml3k.fsf@nosuchdomain.example.com>
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=
 by: Keith Thompson - Fri, 4 Nov 2022 22:05 UTC

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

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

<tk43s4$131p$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23597&group=comp.lang.c#23597

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!uabYU4OOdxBKlV2hpj27FQ.user.46.165.242.75.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: what is the best way to get microseconds of time on Windows in C
or C++ ?
Date: Fri, 4 Nov 2022 22:31:33 +0000
Organization: Aioe.org NNTP Server
Message-ID: <tk43s4$131p$1@gioia.aioe.org>
References: <tk1p0t$1iv03$1@dont-email.me> <tk3qci$112r$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="35897"; posting-host="uabYU4OOdxBKlV2hpj27FQ.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.4.1
X-Notice: Filtered by postfilter v. 0.9.2
 by: Bart - Fri, 4 Nov 2022 22:31 UTC

On 04/11/2022 19:49, Bonita Montero wrote:

> #pragma once
....

Did you write 800 lines of code just for this task?

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

<tk4432$21usu$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23598&group=comp.lang.c#23598

  copy link   Newsgroups: comp.lang.c comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: lynnmcgu...@gmail.com (Lynn McGuire)
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, 4 Nov 2022 17:35:14 -0500
Organization: A noiseless patient Spider
Lines: 52
Message-ID: <tk4432$21usu$1@dont-email.me>
References: <tk1p0t$1iv03$1@dont-email.me> <tk2fch$13ej$1@gioia.aioe.org>
<tk3oto$1va6e$1@dont-email.me> <8735ayml3k.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 4 Nov 2022 22:35:15 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="657e75a71a2bfc0b3f9ae0d2447ebf27";
logging-data="2161566"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+D2CH2ZbaNYxvR7NkgDsWB"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.4.1
Cancel-Lock: sha1:V+7tvpl/H8MaIe2OG/TwMv/tEIQ=
Content-Language: en-US
In-Reply-To: <8735ayml3k.fsf@nosuchdomain.example.com>
 by: Lynn McGuire - Fri, 4 Nov 2022 22:35 UTC

On 11/4/2022 5:05 PM, Keith Thompson wrote:
> 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.

The problem is multiple instances of the same program creating the same
name files.

Lynn

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

<tk45ai$22881$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23599&group=comp.lang.c#23599

  copy link   Newsgroups: comp.lang.c comp.lang.c++
Path: i2pn2.org!i2pn.org!aioe.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: chris.m....@gmail.com (Chris M. Thomasson)
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, 4 Nov 2022 15:56:19 -0700
Organization: A noiseless patient Spider
Lines: 60
Message-ID: <tk45ai$22881$1@dont-email.me>
References: <tk1p0t$1iv03$1@dont-email.me> <tk2fch$13ej$1@gioia.aioe.org>
<tk3oto$1va6e$1@dont-email.me> <8735ayml3k.fsf@nosuchdomain.example.com>
<tk4432$21usu$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 4 Nov 2022 22:56:18 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="729fde6f437813c27b6ae4639ab9a6b8";
logging-data="2171137"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+WxkFyfVpbXSyknBEc/9UGItcq7PORUIQ="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.4.1
Cancel-Lock: sha1:tIHSKVTLABh7F5xI828tqgJdWdg=
In-Reply-To: <tk4432$21usu$1@dont-email.me>
Content-Language: en-US
 by: Chris M. Thomasson - Fri, 4 Nov 2022 22:56 UTC

On 11/4/2022 3:35 PM, Lynn McGuire wrote:
> On 11/4/2022 5:05 PM, Keith Thompson wrote:
>> 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.
>
> The problem is multiple instances of the same program creating the same
> name files.

Prepend the file names with the PID? Something to make it unique.
Perhaps a SHA2-HMAC or something.

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

<87tu3el3ai.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23600&group=comp.lang.c#23600

  copy link   Newsgroups: comp.lang.c comp.lang.c++
Path: i2pn2.org!i2pn.org!aioe.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (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: <tk1p0t$1iv03$1@dont-email.me> <tk2fch$13ej$1@gioia.aioe.org>
<tk3oto$1va6e$1@dont-email.me>
<8735ayml3k.fsf@nosuchdomain.example.com>
<tk4432$21usu$1@dont-email.me>
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=
 by: Keith Thompson - Fri, 4 Nov 2022 23:15 UTC

Lynn McGuire <lynnmcguire5@gmail.com> writes:
> On 11/4/2022 5:05 PM, Keith Thompson wrote:
>> 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.
>
> 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 */

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

<tk4t0o$2abbt$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23601&group=comp.lang.c#23601

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c
Subject: Re: what is the best way to get microseconds of time on Windows in C
or C++ ?
Date: Sat, 5 Nov 2022 06:40:55 +0100
Organization: A noiseless patient Spider
Lines: 10
Message-ID: <tk4t0o$2abbt$1@dont-email.me>
References: <tk1p0t$1iv03$1@dont-email.me> <tk3qci$112r$1@gioia.aioe.org>
<tk43s4$131p$1@gioia.aioe.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 5 Nov 2022 05:40:40 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="9d5ddf26af8849898d3a0e65fe6fccef";
logging-data="2436477"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/m+9Rb5PogLeXTsd1o2VzMSua1+k3Q544="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.4.1
Cancel-Lock: sha1:8veW8IVLKsugrGjtIWW+CHn5tFI=
Content-Language: de-DE
In-Reply-To: <tk43s4$131p$1@gioia.aioe.org>
 by: Bonita Montero - Sat, 5 Nov 2022 05:40 UTC

Am 04.11.2022 um 23:31 schrieb Bart:
> On 04/11/2022 19:49, Bonita Montero wrote:
>
>> #pragma once
> ...
>
>
> Did you write 800 lines of code just for this task?

Dou you think you could get that easier ?

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

<tk93cf$d10$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23605&group=comp.lang.c#23605

  copy link   Newsgroups: comp.lang.c comp.lang.c++
Path: i2pn2.org!i2pn.org!aioe.org!bgz26edc6z/2oKgnjK31sA.user.46.165.242.75.POSTED!not-for-mail
From: ifo...@youknew.org (Opus)
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: Sun, 6 Nov 2022 20:53:50 +0100
Organization: Aioe.org NNTP Server
Message-ID: <tk93cf$d10$1@gioia.aioe.org>
References: <tk1p0t$1iv03$1@dont-email.me> <Je_8L.9313$Iwb3.8230@fx13.iad>
<tk26lf$1moc1$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="13344"; posting-host="bgz26edc6z/2oKgnjK31sA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.4.2
Content-Language: fr
X-Notice: Filtered by postfilter v. 0.9.2
 by: Opus - Sun, 6 Nov 2022 19:53 UTC

Le 04/11/2022 à 06:06, Lynn McGuire a écrit :
> I forgot to add I would prefer a portable way if possible.  I suspect
> that is not possible though below the second resolution.

You're asking specifically on Windows, and then you now want a portable
solution. There isn't. But you could write your own compatibility layer,
calling either Windows API functions or POSIX functions. This isn't
rocket science. I've done that long ago and keep reusing it. There may
of course be many third-party code/libraries doing just that if you
don't want to do it yourself.

IME, using performance counters on Windows is the only sure way of
getting proper resolution. Any third-party code will use them in one way
or another. Why not do that yourself, it'll be only a few lines of code
to write a compatibility layer.

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

<tkuh77$1rt0u$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23653&group=comp.lang.c#23653

  copy link   Newsgroups: comp.lang.c comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: lynnmcgu...@gmail.com (Lynn McGuire)
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: Mon, 14 Nov 2022 16:58:47 -0600
Organization: A noiseless patient Spider
Lines: 30
Message-ID: <tkuh77$1rt0u$1@dont-email.me>
References: <tk1p0t$1iv03$1@dont-email.me> <KG89L.4244$KkB2.2249@fx47.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 14 Nov 2022 22:58:47 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="8ac2b30709a93be5f9dc9e5d0a117911";
logging-data="1963038"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+jAfp21nFpKs7VEXqQmjNi"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.4.2
Cancel-Lock: sha1:VFeedrxRNM8ML7uArDZgZE7mClM=
In-Reply-To: <KG89L.4244$KkB2.2249@fx47.iad>
Content-Language: en-US
 by: Lynn McGuire - Mon, 14 Nov 2022 22:58 UTC

On 11/4/2022 8:44 AM, Scott Lurndal wrote:
> Lynn McGuire <lynnmcguire5@gmail.com> writes:
>> 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
>
> First answer here:
>
> https://stackoverflow.com/questions/10905892/equivalent-of-gettimeday-for-windows

"Nowadys I would use the following for gettimeofday() on Windows, which
is using GetSystemTimePreciseAsFileTime() if compiled for Windows 8 or
higher and GetSystemTimeAsFileTime() otherwise:"

I used the gettimeofday function. Works well !

Thanks,
Lynn

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

<87zgctj8wz.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23654&group=comp.lang.c#23654

  copy link   Newsgroups: comp.lang.c comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (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: Mon, 14 Nov 2022 17:36:28 -0800
Organization: None to speak of
Lines: 34
Message-ID: <87zgctj8wz.fsf@nosuchdomain.example.com>
References: <tk1p0t$1iv03$1@dont-email.me> <KG89L.4244$KkB2.2249@fx47.iad>
<tkuh77$1rt0u$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: reader01.eternal-september.org; posting-host="2b009003a85b55764a4998ccb41247de";
logging-data="1989771"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18hfUoAF7mQX8kHkls7LJLL"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:R4GVULAI8gtDel5fdjCcB0BRCbU=
sha1:at9wP0XfdQcibCnMC9Lt0LpNqZA=
 by: Keith Thompson - Tue, 15 Nov 2022 01:36 UTC

Lynn McGuire <lynnmcguire5@gmail.com> writes:
> On 11/4/2022 8:44 AM, Scott Lurndal wrote:
>> Lynn McGuire <lynnmcguire5@gmail.com> writes:
>>> 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
>> First answer here:
>> https://stackoverflow.com/questions/10905892/equivalent-of-gettimeday-for-windows
>
> "Nowadys I would use the following for gettimeofday() on Windows,
> which is using GetSystemTimePreciseAsFileTime() if compiled for
> Windows 8 or higher and GetSystemTimeAsFileTime() otherwise:"
>
> I used the gettimeofday function. Works well !

Be aware that POSIX says gettimeofday() is obsolescent, and recommends
clock_gettime() instead.

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

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

<tl176i$25jh1$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23655&group=comp.lang.c#23655

  copy link   Newsgroups: comp.lang.c comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: lynnmcgu...@gmail.com (Lynn McGuire)
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: Tue, 15 Nov 2022 17:26:11 -0600
Organization: A noiseless patient Spider
Lines: 75
Message-ID: <tl176i$25jh1$1@dont-email.me>
References: <tk1p0t$1iv03$1@dont-email.me> <KG89L.4244$KkB2.2249@fx47.iad>
<tkuh77$1rt0u$1@dont-email.me> <87zgctj8wz.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 15 Nov 2022 23:26:11 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="2fe837839e01e34d8bda9e918a816fa9";
logging-data="2280993"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/DiJlt3pNKGb4e5cXoZhDD"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.4.2
Cancel-Lock: sha1:PXMONJkvzoRMNTOaIT90S3wp7M0=
Content-Language: en-US
In-Reply-To: <87zgctj8wz.fsf@nosuchdomain.example.com>
 by: Lynn McGuire - Tue, 15 Nov 2022 23:26 UTC

On 11/14/2022 7:36 PM, Keith Thompson wrote:
> Lynn McGuire <lynnmcguire5@gmail.com> writes:
>> On 11/4/2022 8:44 AM, Scott Lurndal wrote:
>>> Lynn McGuire <lynnmcguire5@gmail.com> writes:
>>>> 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
>>> First answer here:
>>> https://stackoverflow.com/questions/10905892/equivalent-of-gettimeday-for-windows
>>
>> "Nowadys I would use the following for gettimeofday() on Windows,
>> which is using GetSystemTimePreciseAsFileTime() if compiled for
>> Windows 8 or higher and GetSystemTimeAsFileTime() otherwise:"
>>
>> I used the gettimeofday function. Works well !
>
> Be aware that POSIX says gettimeofday() is obsolescent, and recommends
> clock_gettime() instead.

This works fine for me:

// from:
//
https://stackoverflow.com/questions/10905892/equivalent-of-gettimeday-for-windows

struct timezone {
int tz_minuteswest;
int tz_dsttime;
};

int mygettimeofday (struct timeval *tv, struct timezone *tz)
{ if (tv) {
FILETIME filetime; /* 64-bit value representing the
number of 100-nanosecond intervals since January 1, 1601 00:00 UTC */
ULARGE_INTEGER x;
ULONGLONG usec;
static const ULONGLONG epoch_offset_us = 11644473600000000ULL; /*
microseconds betweeen Jan 1,1601 and Jan 1,1970 */

#if _WIN32_WINNT >= _WIN32_WINNT_WIN8
GetSystemTimePreciseAsFileTime ( & filetime);
#else
GetSystemTimeAsFileTime ( & filetime);
#endif
x.LowPart = filetime.dwLowDateTime;
x.HighPart = filetime.dwHighDateTime;
usec = x.QuadPart / 10 - epoch_offset_us;
tv->tv_sec = (time_t) (usec / 1000000ULL);
tv->tv_usec = (long) (usec % 1000000ULL);
}
if (tz) {
TIME_ZONE_INFORMATION timezone;
GetTimeZoneInformation(&timezone);
tz->tz_minuteswest = timezone.Bias;
tz->tz_dsttime = 0;
}
return 0;
}

Thanks,
Lynn

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

<tl2k84$2beqi$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23657&group=comp.lang.c#23657

  copy link   Newsgroups: comp.lang.c comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: nos...@gmx.de (Andreas)
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: Wed, 16 Nov 2022 13:15:00 +0100
Organization: A noiseless patient Spider
Lines: 53
Message-ID: <tl2k84$2beqi$1@dont-email.me>
References: <tk1p0t$1iv03$1@dont-email.me> <KG89L.4244$KkB2.2249@fx47.iad>
<tkuh77$1rt0u$1@dont-email.me> <87zgctj8wz.fsf@nosuchdomain.example.com>
<tl176i$25jh1$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 16 Nov 2022 12:15:00 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="f48d101b8c6dfe87b4c83b5caaccc8b9";
logging-data="2472786"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/xndathKnAE5+Pp0iwAZ+H"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.4.2
Cancel-Lock: sha1:x5CTOLbmSGEfmoa1X0om0Qdk0dE=
In-Reply-To: <tl176i$25jh1$1@dont-email.me>
 by: Andreas - Wed, 16 Nov 2022 12:15 UTC

Am 16.11.2022 um 00:26 schrieb Lynn McGuire:
> This works fine for me:
>
>     //  from:
>     //
> https://stackoverflow.com/questions/10905892/equivalent-of-gettimeday-for-windows
>
> struct timezone {
>     int tz_minuteswest;
>     int tz_dsttime;
> };
>
> int mygettimeofday (struct timeval *tv, struct timezone *tz)
> {
>     if (tv) {
>         FILETIME               filetime; /* 64-bit value representing
> the number of 100-nanosecond intervals since January 1, 1601 00:00 UTC */
>         ULARGE_INTEGER         x;
>         ULONGLONG              usec;
>         static const ULONGLONG epoch_offset_us = 11644473600000000ULL;
Wrong value. Correct value is 116444736000000000ULL (9 trailing zeros).

Rough estimation (1970-1601)*365*24*60*60 = 11'636'784'000 seconds
(ignoring Feb 29 in leap years). Additional 6 zeros to convert seconds
to microseconds.

> /* microseconds betweeen Jan 1,1601 and Jan 1,1970 */
>
> #if _WIN32_WINNT >= _WIN32_WINNT_WIN8
>         GetSystemTimePreciseAsFileTime ( & filetime);
> #else
>         GetSystemTimeAsFileTime ( & filetime);
> #endif
>         x.LowPart =  filetime.dwLowDateTime;
>         x.HighPart = filetime.dwHighDateTime;
>         usec = x.QuadPart / 10  -  epoch_offset_us;
>         tv->tv_sec  = (time_t) (usec / 1000000ULL);
>         tv->tv_usec = (long) (usec % 1000000ULL);
>     }
>     if (tz) {
>         TIME_ZONE_INFORMATION timezone;
>         GetTimeZoneInformation(&timezone);
>         tz->tz_minuteswest = timezone.Bias;
>         tz->tz_dsttime = 0;
>     }
>     return 0;
> }
>
> Thanks,
> Lynn
>

Pages:12
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor