程序计时的两种方法

来源:互联网 发布:sql模糊查询[] 编辑:程序博客网 时间:2024/05/14 16:11

一。 调用API函数GetTickCount()

#include <windows.h>
#include <iostream>
int main()
{
 using namespace std;

 DWORD dwStartTick = GetTickCount();
 for (int i=0;i<100;i++)
 {
  cout << i ;
 }

DWORD dwDuration = GetTickCount() - dwStartTick;

  cout << dwDuration; //返回毫秒

return 0;
}

 

二。调用库函数clock()

#include <time.h>
#include <iostream>


int main()
{
 using namespace std;

    double time;
    clock_t tStart = clock();
   
    //do your work..
 for (int i=0;i<100;i++)
 {
  cout << i ;
 }

  
    time = (double)(clock() - tStart) / CLOCKS_PER_SEC;

 cout << time;

 return 0;

}

 

 

原创粉丝点击