我家丫头的cpp

CPP计算某一段程序所花费的时间

2019-03-27  本文已影响0人  李药师_hablee
//计算某一段程序所花费的时间
#include<iostream>
#include<ctime>
using namespace std;

void Delay();//测试函数,用来测试运行时间

int main()
{
    clock_t startT, endT;
    double duration;
    cout<<"Start..."<<endl;
    
    startT = clock();//获取cpu绝对钟数
    Delay();
    endT = clock();
    duration = double(endT - startT)/CLOCKS_PER_SEC;//计算运行的时间,秒为单位
    
    cout<<"程序运行花费的时间为: "<<duration<<endl;
     
    return 0;
 } 
 
 void Delay()
 {
    for(int i = 0;i<1000000000;i++)
    {
        
    }
    return;
 }
输出.PNG
上一篇 下一篇

猜你喜欢

热点阅读