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

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

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

跳至解决方案
896 次查看
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.

 

标记 (2)
0 项奖励
1 解答
882 次查看
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.

在原帖中查看解决方案

2 回复数
883 次查看
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.

876 次查看
Braun
Contributor II

I got it. Thank you.

0 项奖励