0

I would like to measure how long a particular operation takes, therefore I've written the following piece of code:

for (int counter2 = 0; counter2 <= 10; counter2++) { // I'll do the test 10 times,
                                                     // for having reliable results

    time_t ltime;
    time(&ltime); // What's the time at the beginning?

    for (counter = 0; counter <= 1000000; counter++) {
        Do_Something();
    }

    time_t ltime2;
    time(&ltime2); // What's the time after having launched Do_Something()?
    printf("Times:First=[%d], Second=[%d]\n", ltime, ltime2);

}

I'm expecting something like:

Times: First=[1559574983], Second=[1559574985]
Times: First=[1559574990], Second=[1559574999]

Instead I get this:

Times: First=[1559574983], Second=[0]
Times: First=[1559574990], Second=[0]

I've already debugged, and ltime2 seems to be correct. What am I doing wrong?

Dominique
  • 16,450
  • 15
  • 56
  • 112
  • 2
    `time_t` is a `typedef` for an unspecified type, so it's not clear what format string you should use for your platform. Use `std::cout` instead of you want a more portable solution. – François Andrieux Jun 03 '19 at 15:21
  • 2
    Alternatively, use [std::chrono](https://en.cppreference.com/w/cpp/chrono/duration/duration_cast) facilities. – Ron Jun 03 '19 at 15:30
  • 1
    @FrançoisAndrieux Please answer in the answer section where it can be peer reviewed – Lightness Races in Orbit Jun 03 '19 at 15:39
  • 1
    "_for (int counter2 = 0; counter2 <= 10; counter2++) { // I'll do the test 10 times_" - This actually performs the test 11 times. – Ted Lyngmo Jun 03 '19 at 19:24

2 Answers2

4

The format "%d" is not the right one in your case, but you don't have to take care of the type supporting time_t just replace

 printf("Times:First=[%d], Second=[%d]\n", ltime, ltime2);

by

std::cout << "Times:First=[" << ltime << "], Second=[" << ltime2 << "]" << std::endl;

If for an obscure reason you really want to use a C printf as proposed by @Lightness Races in Orbit 3 in a remark you can convert the value to an other type (hopping enough long) and use the format compatible to that type, see Format specifier to print time(0) in C

bruno
  • 32,421
  • 7
  • 25
  • 37
  • And the reason the first "works" is probably something like it's only read the top half of the first argument (and let's presume that the bottom half is zero), due to the dodgy format specifier – Lightness Races in Orbit Jun 03 '19 at 15:38
  • @LightnessRacesinOrbit yes to have that behavior means the size of `time_t` is not the size of an _int_. I edit my answer with the information from your link – bruno Jun 03 '19 at 15:39
  • You could improve your answer by also mentioning the *correct* format string to use for `printf`. OP *may* have good reason to prefer `printf` over `cout` or may just be curious. – Jesper Juhl Jun 03 '19 at 15:40
  • @JesperJuhl Unforts, there isn't one (see linked answer) - I do agree this should be mentioned – Lightness Races in Orbit Jun 03 '19 at 15:41
  • This should be reasonably portable: `printf("Times:First=[%jd], Second=[%jd]\n", static_cast(ltime), static_cast(ltime2));` – Ted Lyngmo Jun 03 '19 at 16:02
  • @TedLyngmo yes, but it is quite masochist to do all that effort when `ostream <<` does that gracefully ^^ – bruno Jun 03 '19 at 16:05
  • @bruno :-) I totally agree. I just put it there since hoping for `long` to be enough may not work on some platforms. `intmax_t` with its `j` modfifier will probably work with all conforming compilers. Unless `time_t` is unsigned... Hmm ... I may need to rethink this. – Ted Lyngmo Jun 03 '19 at 16:08
0

Thanks for the quick replies.
Just for the record, hereby the final solution I'm using (combining the mentioned other Stackoverflow post and difftime() function):

printf("First=[%ju], Second=[%ju], Difference=[%f]\n", (uintmax_t) ltime, 
                                                       (uintmax_t) ltime2, 
                                                       difftime(ltime2, ltime));
Dominique
  • 16,450
  • 15
  • 56
  • 112