美文网首页我家丫头的cpp
CPP计算某一段程序所花费的时间

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

作者: 李药师_hablee | 来源:发表于2019-03-27 14:06 被阅读0次
//计算某一段程序所花费的时间
#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

相关文章

网友评论

    本文标题:CPP计算某一段程序所花费的时间

    本文链接:https://www.haomeiwen.com/subject/wrnavqtx.html