计算一下你的代码有多快

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

#define N  10000

using namespace std;

bool algorithm_count();
int main ()
{
 time_t start,end;
 double diff=0.0;

 time(&start);
 
 if (algorithm_count())//运行被测代码
 {
  time(&end);
  diff=difftime(end,start); 
  cout << "该代码段运行周期: "<<diff<< "\n"<< endl;
  cin.get();
  return 0;
 }
 else
 {
  cout<<"algorithm_count error";
  cin.get();
  return 0;
 } 
 
}

 

 

bool algorithm_count()
{
 int i,j,k,count=0;

 for (i=0;i<N;i++)
 {
  for (j=0;j<N;j++)
  {
   for (k=0;k<N;k++)
   {
    count++;
    //cout<<" i ="<< i<<"\t"<<" j ="<< j<<"\t"<<" k ="<< k<<"\t"<<" count ="<<count<<endl;
   }
  }
 }

 cout<<"\n" <<"N = "<< N<< "\n"<< endl;
 cout <<"count = "<< count<< "\n"<< endl; 
 return true;
}

posted @ 2011-09-07 15:52  lalalalalalalalala  阅读(160)  评论(0编辑  收藏  举报