This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

<time.h> clock() HARD FAULT

Trying to see how much time my code consumes, I tried the naive approach, calling clock() in the beginning and at the end and comparing the values.

But when I include clock() on my code, it just breaks. The function wasn't even called. Why is that?

The code I'm trying:

clock_t begin, end;

begin = clock();
time_consuming_task();
end = clock();

printf("%f ms \n", ((float)(end - begin)) /16000000);
  • If You are using RTC in Your project, You can just check it's registers value to see time passing. Check app_timer.h library. Or You can set/clear GPIO and measure time on oscilloscope...

  • It should not have hardfaulted. Are you sure that clock() function is causing it?

  • I comment the line, code works fine. I don't know what happened.

  • where's the implementation of clock()? Sure it's in the standard c library but I can't see how it could possibly be implemented on something with no operating system, no real time clock, no gettimeofday(), no nothing. I'm fairly surprised it links, I'm not at all surprised it fails.

  • @RK, i think the clock() just return the current tick which is updated in systems with operating systems. Here as there is no OS, it will never get updated and hence will return the same value always. It would compile as the variable do exist in the library, just that the mechanism to update it does not. Using clock() is of no use here.

1 2