
Charles B. answered 06/15/19
Passionate Professional Programmer
The other options you have depend on both the OS you're running and the version of C you're targeting. "gettimeofday" and "clock_gettime" might help you. But you also have sub-second resolution with the method you have and here's why:
If you look at CLOCKS_PER_SEC it's defined as 1,000,000. So what that means is you have a resolution down to 1/1,000,000th of a second, or 1 micro second. Which is to say, running "clock()" before and after your program does its work effectively keeps track of the number of microseconds your program is spending in the processor. So if your program tracks one clock tick it took a microsecond. 2,500,000 clock ticks? 2.5 seconds. 10,000 ticks? 0.01 second (or 10 milliseconds).
The downside to using the clock() function could be that it is not related to wall-clock time, but to processor time. Which means that if you have a lot of other things competing for processor time you might see a very low number from your computation because your program isn't getting much processor time, even if it took several minutes because your program was being starved of CPU time. This actually gives you a better picture of whether a change makes a difference in the performance of your program as opposed to in your java program you might just be tracking how busy your CPU is.