Hi Bruno! > Le 4 oct. 2018 à 18:51, Bruno Haible <br...@clisp.org> a écrit : > > […] > > I would conclude that either times() or getrusage()+gettimeofday() > provides the necessary functionality. Which one to choose? I think > it's useful to compare the results on various platforms (regarding > precision and behaviour regarding children processes), to see > which one is better.
With respect to wall clock: - nothing in getrusage. You suggest gettimeofday, but it’s not monotonic, clock_gettime(CLOCK_MONOTONIC,…) is a better option. It has portability issues, but gnulib to the rescue. - times returns a clock_t, in system dependent clock ticks. Resolution is unclear, but I think I understood from various place that it’s typically 1µs. https://www.gnu.org/software/libc/manual/html_node/Processor-Time.html With respect to what you called multiplicity (i.e., time spent in subprocesses or threads): - getrusage offers two choices: - RUSAGE_SELF: the calling process and its threads. - RUSAGE_CHILDREN: main and subprocesses that have been wait’ed. - Linux offers RUSAGE_THREAD, which is just about the current thread. I don’t think we care about this. (https://linux.die.net/man/2/getrusage) - times offers the same options: - tms_utime/tms_stime: user/sys CPU time of the calling process. - tms_cutime/tms_cstime: user/sys sum of the tms_stime/tms_cstime of wait’ed child processes. (http://pubs.opengroup.org/onlinepubs/9699919799/functions/times.html) With respect to precision: - getrusage: 1µs on modern platforms, possibly 1ms (https://stackoverflow.com/a/12480485/1353549) - times: clock ticks (as for its usr/sys times), so I believe, 1µs in practice With respect to portability: - getrusage is handled by gnulib - times too, but AFAICT, only for Windows (which is probably the sign that’s it’s more portable than getrusage. It seems that both choices are fairly equivalent. That’s what I can see looking at macOS and GNU/Linux. I have no idea about the other platforms.