程序时间测定

来源:互联网 发布:java抽象方法的作用 编辑:程序博客网 时间:2024/06/09 19:57

利用clock()获取cpu的计时单元数,来完成计算程序运行时间

#include<iostream>#include<ctime>#include<string>using namespace std;int main(void){    clock_t begin,over;    begin = clock();//获取开始时的cup计时单元数    string str = "henuzxy";    for(int i=1;i<=100000000;i++){        int ans = (int)str.length();    }    over = clock();//获取结束时的cup计时单元数    cout << (double)(over - begin)/CLOCKS_PER_SEC  << " (s) "<< endl;    //二者相减就是cup的运行周期数,CLOCKS_PER_SECS是cup一秒的运行周期数    return 0;}
阅读全文
0 0
原创粉丝点击