Why clock() didn't work properly in a ‘serial’ terminal like PuTTY?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Why clock() didn't work properly in a ‘serial’ terminal like PuTTY?

Jump to solution
864 Views
Braun
Contributor II

I want to use clock() to calculate the approximation of the cumulative amount of time used by the program since it started and here is part of my lines in main():

start = clock();
PRINTF("%d \n", start);

EightQueen(0,q); // it is a subfunction, I want to calculate the duration of it.
PRINTF("A total of %d solutions!\r\n", count);

end = clock();
PRINTF("%d \n", end);

PRINTF("%d s\n", (end-start)/CLOCKS_PER_SEC);

When I run the same program in my LPC812,  it prints the result in the MCUXpresso's console, it print the right result of the time. 

Braun_0-1626841918212.png

But when print in the PuTTY, ‘serial’ terminal. It prints a totally wrong result.The start time and the end time are exactly the same and whatever the real duration of the program should be, the start always equals to the end and exactly equals to 32.

Braun_1-1626842247223.png

 

I want to know how to fix it, thank you.

 

Tags (2)
0 Kudos
1 Solution
850 Views
converse
Senior Contributor V

clock() is a semihosted function - it is implemented on the host. So, it will only ever work when you are running and connected to a debugger (otherwise it will return zero). If you want to calculate time, accurately, on your device, you will need to use one of the timers provided on your device.

View solution in original post

2 Replies
851 Views
converse
Senior Contributor V

clock() is a semihosted function - it is implemented on the host. So, it will only ever work when you are running and connected to a debugger (otherwise it will return zero). If you want to calculate time, accurately, on your device, you will need to use one of the timers provided on your device.

844 Views
Braun
Contributor II

I got it. Thank you.

0 Kudos