0

I'm actually doing a program to test different sorting algorithms. However, as I try to time the code execution, it prints only 3 decimals as you can see in the portion of output that I've pasted, and I don't know why.

code.c

//insertion sort test
time_t t = 0;
t = clock();
insertion(arr, n);
t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC; // in secondi
printf("insertion() took %f seconds to execute\n", time_taken);
fprintf(fp, "%f,", time_taken);
fflush(fp);

data.csv

0.001000,0.000000,0.000000,0.001000,0.000000
0.002000,0.000000,0.000000,0.002000,0.000000
0.005000,0.000000,0.000000,0.005000,0.000000
0.008000,0.001000,0.000000,0.008000,0.000000
0.004000,0.000000,0.000000,0.021000,0.000000
0.011000,0.000000,0.000000,0.024000,0.001000
0.008000,0.016000,0.000000,0.025000,0.000000
0.031000,0.001000,0.001000,0.029000,0.000000
0.041000,0.001000,0.000000,0.033000,0.000000
0.060000,0.001000,0.001000,0.051000,0.001000
0.062000,0.001000,0.000000,0.059000,0.001000
0.074000,0.002000,0.001000,0.068000,0.000000
0.085000,0.002000,0.001000,0.080000,0.000000

I'm on Windows 11, with AMD Ryzen 5 processor. Maybe it is because of some configuration in the hardware or in the OS.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    What value is `CLOCKS_PER_SEC`? What precision do you excpect to get and why? – Gerhardh Nov 03 '22 at 17:28
  • 2
    It is printing 6 decimal places. If `CLOCKS_PER_SEC` is `1000` (as with MSVC) then you'll get units of 1/1000 second, i.e. `0.001`. – Weather Vane Nov 03 '22 at 17:29
  • use `clock_gettime()` - see https://stackoverflow.com/a/53708448/1579327 – Paolo Nov 03 '22 at 17:36
  • @Paolo: That is a POSIX function, but OP stated that they are using Microsoft Windows 11. – Andreas Wenzel Nov 03 '22 at 17:38
  • If you want higher precision on Microsoft Windows, you may want to take a look at the platform-specific function [`QueryPerformanceCounter`](https://learn.microsoft.com/en-us/windows/win32/api/profileapi/nf-profileapi-queryperformancecounter). – Andreas Wenzel Nov 03 '22 at 17:40
  • 1
    @AndreasWenzel the standard C equivalent to `clock_gettime()` is [`timespec_get()`](https://en.cppreference.com/w/c/chrono/timespec_get) – phuclv Nov 03 '22 at 17:43
  • You can use Performance counters: [`tbeg(),tend()` from here](https://stackoverflow.com/a/21509808/2521214) to measure time on windows with better precision than just `[ms]` ... if that is not enough you can use RDTSC instruction on x86/x64 compatible CPUs – Spektre Nov 04 '22 at 07:39

1 Answers1

2

Try running this:

#include <time.h>
#include <stdio.h>

int main () {
   printf("CLOCKS_PER_SEC: %ld", CLOCKS_PER_SEC);
   return(0);
}

If CLOCKS_PER_SEC shows as 1000 then this explains why the precision is limited to 3 decimal places.

  • Yes, it actually print 1000, what does it mean? – Grovetender Nov 03 '22 at 17:59
  • `CLOCKS_PER_SEC` means "clock ticks per second". In one second the count returned by `clock()` advances by `CLOCKS_PER_SEC`, here `1000`. It means the *units* in which the clock operates. The actual amount varies with the operating system, which is why that definition is provided for you. You divided the **count difference** by `CLOCKS_PER_SEC` and it gave you the **time difference** in seconds. – Weather Vane Nov 03 '22 at 18:12
  • @Grovetender: See the documentation for [`CLOCKS_PER_SEC`](https://en.cppreference.com/w/c/chrono/CLOCKS_PER_SEC). – Andreas Wenzel Nov 03 '22 at 18:56
  • Without knowing anything about semantics of `CLOCKS_PER_SECOND` it already tells you that dividing an integer value (`time_t`) by `CLOCKS_PER_SECOND` cannot result in anything with more than 3 digits after the `.` – Gerhardh Nov 04 '22 at 09:05
  • On my Macbook Pro CLOCK_PER_SEC is 1000000. I'm surprised your Windows 11 with AMD Ryzen 5 is only 1000, which is limiting the precision you can get in timing tasks. – Anthony Kelly Nov 04 '22 at 15:38