2

I'm trying to clock a sorting function, pretty straightforward stuff. Code looks like:

clock_t start_t, end_t, total_t;
start_t = clock();
sort();
end_t = clock();
total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC;
printf("sort time: %le", (double)total_t);

You could say this works. But, no matter how long it takes I will get an integer output. If sorting 1M elements prints out

sort time: 1.000000e+00

And sorting 2M elements prints out

sort time: 2.000000e+00

Then sorting any value of elements between the two will print one of those two outputs. 1.3M elements also takes 1.000000e+00 seconds apparently.

Why is this happening and more importantly how do I fix it?

Colin Harrison
  • 184
  • 2
  • 18
  • 10
    Naming variables `xxxx_t` is highly misleading as it's a convention for naming types. – Eugene Sh. Mar 26 '18 at 17:24
  • Straightforward? No, not really. Different platforms assign different semantics to `clock()`, – n. m. could be an AI Mar 26 '18 at 17:31
  • You are rounding the output to the number of clocks per second, so your output will always be rounded to seconds total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC; Should be total_t = (double)(end_t - start_t); – guilleamodeo Mar 26 '18 at 17:43
  • 1
    @guilleamodeo `(double)(end - start) / CLOCKS_PER_SEC` always gives you the number of seconds elapsed between the two times - `(double)(end - start)`, on the other hand, isn't guaranteed to be any useful value. – hnefatl Mar 26 '18 at 17:51
  • Ooops! I didn't mean that, I am missing a chunk in there (itmust sent the post when I pressed the keys to paste instead of pasting) . What I meant is that you need to convert CLOCKS_PER_SEC to double as well... – guilleamodeo Mar 26 '18 at 18:17
  • `total_t` is an integer variable... you are converting to integer the `double` computed value. – Luis Colorado Mar 28 '18 at 20:38

1 Answers1

4

You assign the result of a double computation to total_t of type clock_t, which may truncate the result as clock_t is usually an integer type (not guaranteed, but seemingly so on your machine) - it doesn't matter that you then cast back to double afterwards when printing.

Just store your result in a double instead, as recommended here:

double total = (double)(end - start) / CLOCKS_PER_SEC;
printf("sort time: %le", total);
hnefatl
  • 5,860
  • 2
  • 27
  • 49
  • Using a `long double` really seems like overkill. Do you really need this computation to be that accurate? – rici Mar 26 '18 at 19:34
  • @rici It was mostly on the recommendation of the post I linked. Prior to your comment I also thought that `long double` was around 64 bits so wouldn't cost much extra, but I've just found out that it's more commonly 80. I'll change this back to `double` as I can't find a compelling reason to use `long double`. – hnefatl Mar 26 '18 at 21:09
  • @rici, indeed the resolution is `CLOCKS_PER_SEC`, so probably there are no more than 1000 clocks in a second... so a `float` should suffice. – Luis Colorado Mar 28 '18 at 20:40