测试代码的运行时间(经典加总结)

C++版测试代码运行时间(第一种方法)

# include<iostream>
# include<ctime>
# include<cstdlib>
# include<algorithm>
# include<cstdio>
using namespace std;
int main()
{
    srand((unsigned)time(0));
   //freopen("d:\\A.in","r",stdin);
    //freopen("d:\\A.out","r",stdout);
    int begin,end;
    begin=clock();

    int a,b;
    scanf("%d %d",&a,&b);
    int c = a+b;
    printf("%d",c);

    end=clock();
    cout<<"runtime: "<<double(end-begin)/CLOCKS_PER_SEC<<endl;
}
View Code

C++版测试代码运行时间(第二种方法)

# include<iostream>
# include<ctime>
# include<cstdlib>
# include<algorithm>
# include<cstdio>
using namespace std;
int main()
{
    srand((unsigned)time(0));
    //freopen("d:\\A.in","r",stdin);
    //freopen("d:\\A.out","r",stdout);
    time_t begin,end;
    begin=clock();

    int a,b;
    scanf("%d %d",&a,&b);
    int c = a+b;
    printf("%d",c);

   end=clock();
    cout<<"runtime: "<<double(end-begin)/CLOCKS_PER_SEC<<endl;
}
View Code

闲来无事写写代码,消磨time。。。。。

posted @ 2013-09-05 13:46  天天AC  阅读(341)  评论(0编辑  收藏  举报