public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "frankhb1989 at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libstdc++/63400] [C++11]precision of std::chrono::high_resolution_clock
Date: Mon, 29 Sep 2014 21:00:00 -0000 [thread overview]
Message-ID: <bug-63400-4-2fLqO5NKxy@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-63400-4@http.gcc.gnu.org/bugzilla/>
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63400
--- Comment #4 from frankhb1989 at gmail dot com ---
(In reply to Jonathan Wakely from comment #3)
> What libstdc++ is doing is sensible, why is the realtime clock so much
> coarser than the monotonic clock on mingw-w64?
It is not always true that the real time clock would have a higher resolution
than monotonic clock. At least I found nothing about resolution guaranteed by
POSIX, in fact, "clock resolutions are implementation-defined".
With mingw-w64, the following program shows that the monotonic clock is far
more precise:
#include <pthread_time.h>
#include <iostream>
int main()
{
using namespace std;
timespec ts;
if(clock_getres(CLOCK_REALTIME, &ts) == 0)
cout << "CLOCK_REALTIME: " << ts.tv_sec << ',' << ts.tv_nsec << endl;
if(clock_getres(CLOCK_MONOTONIC, &ts) == 0)
cout << "CLOCK_MONOTONIC: " << ts.tv_sec << ',' << ts.tv_nsec << endl;
}
The result on my machine:
CLOCK_REALTIME: 0,15625000
CLOCK_MONOTONIC: 0,489
I then found the actual implementation in winpthreads (clock.c):
int clock_gettime(clockid_t clock_id, struct timespec *tp)
{
unsigned __int64 t;
LARGE_INTEGER pf, pc;
union {
unsigned __int64 u64;
FILETIME ft;
} ct, et, kt, ut;
switch(clock_id) {
case CLOCK_REALTIME:
{
GetSystemTimeAsFileTime(&ct.ft);
t = ct.u64 - DELTA_EPOCH_IN_100NS;
tp->tv_sec = t / POW10_7;
tp->tv_nsec = ((int) (t % POW10_7)) * 100;
return 0;
}
case CLOCK_MONOTONIC:
{
if (QueryPerformanceFrequency(&pf) == 0)
return lc_set_errno(EINVAL);
if (QueryPerformanceCounter(&pc) == 0)
return lc_set_errno(EINVAL);
tp->tv_sec = pc.QuadPart / pf.QuadPart;
tp->tv_nsec = (int) (((pc.QuadPart % pf.QuadPart) * POW10_9 +
(pf.QuadPart >> 1)) / pf.QuadPart);
if (tp->tv_nsec >= POW10_9) {
tp->tv_sec ++;
tp->tv_nsec -= POW10_9;
}
return 0;
}
case CLOCK_PROCESS_CPUTIME_ID:
{
if(0 == GetProcessTimes(GetCurrentProcess(), &ct.ft, &et.ft, &kt.ft,
&ut.ft))
return lc_set_errno(EINVAL);
t = kt.u64 + ut.u64;
tp->tv_sec = t / POW10_7;
tp->tv_nsec = ((int) (t % POW10_7)) * 100;
return 0;
}
case CLOCK_THREAD_CPUTIME_ID:
{
if(0 == GetThreadTimes(GetCurrentThread(), &ct.ft, &et.ft, &kt.ft,
&ut.ft))
return lc_set_errno(EINVAL);
t = kt.u64 + ut.u64;
tp->tv_sec = t / POW10_7;
tp->tv_nsec = ((int) (t % POW10_7)) * 100;
return 0;
}
default:
break;
}
return lc_set_errno(EINVAL);
}
For CLOCK_REALTIME, the Windows API GetSystemTimeAsFileTime is used.
GetSystemTimePreciseAsFileTime is an improved version which provide "the
highest possible level of precision (<1us)". Unfortunately, the latter is only
available since Windows 8/Windows 2012, which is not suited for winpthreads for
compatibility reason IMO. See this MSDN page for details:
http://msdn.microsoft.com/en-us/library/windows/desktop/hh706895(v=vs.85).aspx
For CLOCK_MONOTONIC, QPC(Query Performance Counter) APIs are used. This method
is reasonably portable (among different versions of Windows, since Windows
2000), and Microsoft strongly suggested it when high-resolution time stamps
needed, see
http://msdn.microsoft.com/en-us/library/windows/desktop/dn553408(v=vs.85).aspx.
However, QPC is not suited for a system-wide clock.
next prev parent reply other threads:[~2014-09-29 21:00 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <bug-63400-4@http.gcc.gnu.org/bugzilla/>
2014-09-29 9:33 ` redi at gcc dot gnu.org
2014-09-29 17:18 ` redi at gcc dot gnu.org
2014-09-29 21:00 ` frankhb1989 at gmail dot com [this message]
2014-09-29 21:03 ` frankhb1989 at gmail dot com
2014-09-30 10:36 ` redi at gcc dot gnu.org
2022-07-14 18:17 ` redi at gcc dot gnu.org
2022-07-14 18:19 ` redi at gcc dot gnu.org
2022-07-15 10:37 ` redi at gcc dot gnu.org
2024-03-08 10:21 ` redi at gcc dot gnu.org
2024-03-08 10:21 ` redi at gcc dot gnu.org
2024-03-08 11:41 ` redi at gcc dot gnu.org
2024-03-08 13:47 ` vz-gcc at zeitlins dot org
2024-03-08 14:37 ` redi at gcc dot gnu.org
2024-03-08 14:38 ` redi at gcc dot gnu.org
2024-03-08 15:03 ` vz-gcc at zeitlins dot org
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=bug-63400-4-2fLqO5NKxy@http.gcc.gnu.org/bugzilla/ \
--to=gcc-bugzilla@gcc.gnu.org \
--cc=gcc-bugs@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).