-2

Is there a UNIX time equivalent in C++, which gives real, user and system time of execution preferably within main? Or are there non-trivial ways to use chrono, etc to achieve the same functionality?

Could not find chrono usage to record all three timestamps, but only the real time based on timeclock link.

Aiming to walk away from redirection operator coupled with executable and trying to embed time logic within main itself. time a.out 2> log.txt

5
  • 1
    Please show us some code to illustrate. Btw; what's wrong with std::chrono? Apr 20, 2019 at 19:11
  • Not in Standard C++. C++ is a big tent and not all systems make time available in this manner. There might be a Unix C call that provides this, but my usefulness to you ends at this point. Apr 20, 2019 at 19:31
  • 1
    Quick google finds: linux.die.net/man/2/times Apr 20, 2019 at 19:34
  • 1
    @JesperJuhl give the question a re-read, my question was different and there is a snippet on redirection operator which I attempt to walk away from. Apr 20, 2019 at 19:37
  • 1
    It's customary to not add any kind of "solved" marks to the title. Instead you should accept your answer when you'll be able to (after the timeout). Apr 20, 2019 at 23:34

1 Answer 1

4

Solved using sys/times.h and clock_t.

For both methods, system time sys was recorded using times.h, and clock_t's REALTIME was more accurate for user recording.

#include <iostream>
#include <sys/times.h>

std::string parse_tim(intmax_t milliseconds) {
  std::string parsed;

  intmax_t s = milliseconds / 1000;
  intmax_t m = s / 60;
  intmax_t h = m / 60;
  intmax_t d = h / 24;

  if(d > 0)
    parsed = parsed + std::to_string(d) + "d";
  if(! parsed.empty() || h > 0)
    parsed = parsed + std::to_string(h%24) + "h";
  if(! parsed.empty() || m > 0)
    parsed = parsed + std::to_string(m%60) + "m";

  parsed = parsed + std::to_string(s%60) + ".";
  parsed = parsed + std::to_string(milliseconds%1000) + "s";

  return parsed;
}

int main(int argc, char** argv) {

  static struct tms st_cpu, en_cpu;
  struct timespec ts1, tw1, ts2, tw2;

  static clock_t st_time = times(&st_cpu);
  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts1);
  clock_gettime(CLOCK_REALTIME, &tw1);


  // Perform operations
  // Perform operation
  // Perform actions


  static clock_t en_time = times(&en_cpu);
  clock_gettime(CLOCK_REALTIME, &tw2);
  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts2);

  auto real_clock = (intmax_t)(en_time - st_time) * 10;
  auto user_clock = (intmax_t)(en_cpu.tms_utime - st_cpu.tms_utime) * 10;
  auto sys_clock = (intmax_t)(en_cpu.tms_stime - st_cpu.tms_stime) * 10;
  std::cerr << "time.h\treal\t" << parse_tim(real_clock) << "\t"
            << "user\t" << parse_tim(user_clock) << "\t"
            << "sys\t" << parse_tim(sys_clock) << std::endl;

  auto real_dur = intmax_t(1000.0*tw2.tv_sec + 1e-6*tw2.tv_nsec - (1000.0*tw1.tv_sec + 1e-6*tw1.tv_nsec));
  auto user_dur = intmax_t(1000.0*ts2.tv_sec + 1e-6*ts2.tv_nsec - (1000.0*ts1.tv_sec + 1e-6*ts1.tv_nsec));
  auto sys_dur = intmax_t(en_cpu.tms_stime - st_cpu.tms_stime) * 10;
  std::cerr << "clock_t\treal\t" << parse_tim(real_dur) << "\t"
            << "user\t" << parse_tim(user_dur) << "\t"
            << "sys\t" << parse_tim(sys_dur) << std::endl;

  return 0;
}

time ./a.out

time.h    real  4m2.460s    user    5m57.140s   sys 21.830s
clock_t   real  4m2.457s    user    6m18.976s   sys 21.830s

real    4m2.498s
user    6m22.816s
sys     0m26.865s

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.