2

I am trying to create a timing function to test the initialization speed of a product. I have been trying to use chrono or time.h, but chrono fails in C because it uses C++ standard definitions and all I can find for time.h requires a g++ version running on linux. Does anyone know of a way to get nanosecond resolution timing in C on Windows?

John B
  • 43
  • 5
  • 1
    [High Resolution Timer function in C/C++](https://www.roxlu.com/2014/047/high-resolution-timer-function-in-c-c--) – Robert Harvey May 23 '22 at 18:46
  • 3
    This is called an XY problem. You encountered one problem, and now you try to solve it in a specific way, needlessly complicating things. Take it one step back: what is your problem with [``](https://en.cppreference.com/w/c/chrono) (which *does not* require C++)? Anyway, you do not need nanosecond resolution. At that kind of resolution, measuring "perfornance" is mostly meaningless (and most computers will not give you nanosecond *precision* anyway). XY problem again, the question should be "how do I measure performance effectively". – DevSolar May 23 '22 at 18:46
  • See [Acquiring high-resolution time stamps](https://learn.microsoft.com/en-us/windows/win32/sysinfo/acquiring-high-resolution-time-stamps) – Weather Vane May 23 '22 at 18:49
  • @RobertHarvey thank you! this is exactly what I needed, versatile OS independent timer with high resolution – John B May 23 '22 at 18:56
  • @DevSolar ah apologies, for some reason the word chrono was cut off after I added tags, time.h doesnt use c++ but it had os dependent implementatiosn – John B May 23 '22 at 18:59
  • @DevSolar the information in your comment is helpful, but this is the opposite of an XY problem: OP explained exactly what is attempted. – Elazar May 23 '22 at 19:05
  • @JohnB So does any alternative. Time *is* managed by the OS; any solution eventually has to rely on implementation details, unless you are looking at embedded environments. Again, note that 1) *resolution* and *precision* are different things, 2) you don't get nanosecond *precision* from your average desktop, and 3) if you think you need that kind of precision to measure any kind of performance, you are likely looking at the wrong thing (like measuring a single run of some fast function, which will not give you useful answers). – DevSolar May 23 '22 at 19:18
  • I'm with DevSolar. What code constitutes "initialization" in your phrase "initialization speed of a product"? If initialization code executes so quickly that you need even microsecond resolution, then that implies the initialization code is executing frequently. If initialization code is not executing frequently then you shouldn't care even if it takes a millisecond. Most people can't perceive anything faster than a couple of millis anyway. – Jeff Holt May 23 '22 at 19:25
  • @JeffHolt I agree with him too, if you wanted to know why the customer needs nanosecond timing youd have to ask them. Products are sold to magnetic resonance research labs around the world though so it's not the first time I've had to deal with a strange request – John B May 23 '22 at 19:31
  • 1
    Long time that I played with this, but at the time I found that rdtsc takes about 40ns to execute. So you’ll never get nanosecond resolution. – gnasher729 May 23 '22 at 19:40
  • See: https://stackoverflow.com/questions/5404277/porting-clock-gettime-to-windows – Craig Estey May 23 '22 at 19:53
  • 1
    @DevSolar Many systems have nanosecond resolution clock sources: x86 has `rdtsc` [accessed via `clock_gettime`]. arm has a tsc equivalent (control reg) and also accessed via `clock_gettime`. Hires timestamps are critical for embedded systems dealing with H/W to measure performance and _latency_, particularly with multithreading. For a simple example, see my answer from a half hour ago: https://stackoverflow.com/questions/72353310/c-program-hanging-when-executing-thread-in-producer-consumer-problem/72353838#72353838 – Craig Estey May 23 '22 at 20:01
  • Just a side note: on MacOS, there is (now) `clock_gettime()` which has nanosecond resolution in the data structure but [`clock_getres()`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_getres.html) informs you that the resolution is 1000 nanoseconds, aka 1 microsecond. That is, the last three digits (of the decimal representation of the nanoseconds number) will always be zero. That's allowed by POSIX. Also, standard C11 and later has the [`timespec_get()`](https://port70.net/~nsz/c/c11/n1570.html#7.27.3) function — but support for that may not be widely available. – Jonathan Leffler May 23 '22 at 20:22
  • Yup - you need special hardware to get accuracy:(( – Martin James May 24 '22 at 00:32

0 Answers0