参考:
C语言的clock()测出来的是什么时间 https://zhidao.baidu.com/question/1368126684739615259.html
请问C语言中clock()函数该怎么用? https://zhidao.baidu.com/question/1368126684739615259.html
“clock函数返回的是cpu时间,并不是秒数,真正的一秒钟可能包含若干个CPU时间,这个值通常是由宏CLOCKS_PER_SEC来定义,表示一秒中有CLOCKS_PER_SEC这么多个cpu时间,不同的编译器可能不同,比如,linux C中,它是1000000,在VC6中,它是1000,你用cost除以CLOCKS_PER_SEC即可得到实际的秒数。”
IDE:VS2010
结果输出:1.234s
#include <iostream>
#include <time.h>
#include <windows.h>
using namespace std;
int main(){
clock_t start, end;
start = clock();
Sleep(1234); //Sleep()需要windows.h
end = clock();
cout<<(float)(end-start)/CLOCKS_PER_SEC<<"s"<<endl;
return 0;
}