Linux??clock?????????
???????????? ???????[ 2016/2/18 16:32:38 ] ??????????????
????????Linux??Winows????б??????????????????????????Щ????
????1. ??????????????clock()???????
????????????μ??????????????????????????????????????????????????????????
int main(int argc?? char **argv)
{
clock_t t1=clock();
ifstream in("data.txt");
vector<int> v;
for(int a;in>>a;v.push_back(a));
cout<<v.size()<<endl;
for(int i=0;i<v.size();i++)
cout<<v[i]<<" ";
cout<<endl;
clock_t t2=clock();
cout<<"TotalTime:"<<t2-t1<<"ms"<<endl;
}
??????δ?????????????clock_t?????clock()?????????????????????
??????windows?±??????У?
????TotalTime:465ms
??????Linux?±??????У?
????TotalTime:420000ms
????ok????????????????????????????????????????????????clock()???????????μ??????????????????clock?????????????????????
????clock returns the processor time used by program since the beginning of the execution?? or -1 if unavailable.
??????????clock()?????????????????й????к???process time?????CPU time??
???????????????????????????λ??????????????????????????
????clock() / CLOCKS_PER_SEC is a time in seconds.
????CLOCKS_PER_SEC??????????????????ж?????????????????????δ?????
?????????????????δ?????????????clock()?????????????(clock tick)?????????????????????clock tick????CPU?????????????????C/C++??????????????λ??????????????й????TC2.0??????18.2???δ????????VC??????1000???δ??????????POSIX?ж????1000000???δ?????
????ok????????????????ó?????????????????????????????????????????′????
????cout<<"TotalTime:"<<(double)( t2-t1)/CLOCKS_PER_SEC<<"s"<<endl;
??????????????????????????????????????в???????????????????????????????10????????????????????ζ???????????????????????POSIX????clock()???????10ms???????????????????????
????2. ?????????????????????????
????????????????????????????clock()?????????????????????????????????????????????ɡ??????????????????????е??????????????????????????????????????????????????????????????????Щ????????????????????????????????
????????????“???????????????????????????????????????????仰????????????????????????????????????????????????????????????????????ж??й????????????????????????£?????????????????????????????????????л????????ж?????????????????????????????????????????????????????????
???????????????????????????????????????????????ж???????????????????????????????????????????????????????л????л????????????????????????????л??????????????A?л???????B?????????????????????????A???????棬?????????B??????????????л???????????????????????????嵽????????????????????????????????????????????????л?????????????????????????????????????”??
????ok????????????λ???????ú?????????????????????????????????????????????????????????仨???????????????????????????????windows????linux???????????????????????????????????????????????????????????????????з??????£?
????(1) ??????????real time??????????????е?????????????????
????(2) ???CPU???user CPU time???????????????????????????
????(3) ??CPU???system CPU time??????????????????????????????
??????????????????????????????????(1)????????(2)+(3)?????????????????????????????????????б??????
??????????????????????????????????????????????????????γ?????????????????????????????????????????????????д???CPU????????????
????ok?????????????????δ???????н????clock()???????便??????????????????????????????????????飺
??????δ??????????????ο??????????????
int main( void )
{
clock_t start?? finish;
double duration;
long i??j;
start = clock();
for( i=0;i<100;i++){
for( j=0;j<1000000;j++)??
}
finish = clock();
duration = (double)(finish- start) / CLOCKS_PER_SEC;
printf( "Time to do %ld empty loops is "??i*j);
printf( "%f seconds
"??duration );
return 0;
}
????????????
????g++ test.cpp -o test
???????г???
????./test
??????????
????Time to do 100000000 empty loops is 0.290000 seconds
????????????????????????????e???????????м????
????sleep(5); // ???????????<unistd.h>
????sleep?????????????????????????????????5s???CPU??????y???????????????????????????5s????????????clock?????????????????????????????????290ms?????????CPU???????
???????г??? ./test
??????????
????Time to do 100000000 empty loops is 0.290000 seconds
??????????????????clock??????????CPU??????????????????????????????????????????????clock???????
?????????????????processor time??????CPU??????e???????????????????????????????????clock??????????????CPU?????????????????????
??????????????????????????????а????????????????????????????????е??
????Linux??????????????????????????????????е??????????——time?????????????????????????????
????????????????估??????????????????????????С?ξ?????????????????????????
??????????time???????????????β??????
????????shell????
????time ./test
??????????
????Time to do 100000000 empty loops is 0.290000 seconds
????real 0m0.915s
????user 0m0.031s
????sys 0m0.266s
???????????????????????????????????????????clock()????????????????????CPU?????????clock????????????????????????
????????????time????????????????????????????????????????????????????????????????????????????????
????????????????????????????????????????
???????????ü????????????????????????????????ж????????????????????????б????????????????????
?????????????A?????????????????A?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????β??????????????????????????????????????????????????Щ??????Щ????????????????á??????????????????????????????????г???????????????????????????????????????塣?????????????????????????????????????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11