64,652
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
#include<vector>
#include<string>
#include<time.h>
using namespace std;
int seqsearch(int *a,const int n,const int x)
{
int i=n;a[0]=x;
while(a[i]!=x)i--;
return i;
}
int TimeSearch(const long m)
{
int a[1001],n[20];
for(int j=1;j<=1000;j++)a[j]=j;//初始化a
for(int j=0;j<10;j++) //n的取值
{
n[j]=10*j;n[j+10]=100*(j+1);
}
cout<<"n 总时间 运行时间"<<endl;
for(int j=0;j<20;j++)
{
long start,stop;
time(start);
for(long b=1;b<=m;b++)
{
int k=seqsearch(a,n[j],0);
}
time(stop);
long totalTime=stop-start;
float runTime=(float)(totalTime)/(float)m;
cout<<" "<<n[j]<<" "<<totalTime<<" "<<runTime<<endl;
}
return 1;
}
int main()
{
TimeSearch(3000);
return 0;
}
#include<iostream>
#include<time.h>
using namespace std;
int seqsearch(int a[],const int n,const int x)
{
int i=n;
a[0]=x;
while(a[i]!=x)i--;
return 1;
}
int TimeSearch(const long m)
{ long* start=new long [],*stop=new long [];
int a[1001],n[20];
for(int j=1;j<=1000;j++)
a[j]=j;//初始化a
for( j=0;j<10;j++) //n的取值
{
n[j]=10*j;n[j+10]=100*(j+1);
}
cout<<"n 总时间 运行时间"<<endl;
for(j=0;j<20;j++)
{long* start=new long [],*stop=new long [];
time(start);
for(long b=1;b<=m;b++)
{
int k=seqsearch(a,j,0);
}
time(stop);
long totalTime=(long)stop-(long)start;
float runTime=(float)(totalTime)/(float)m;
cout<<" "<<n[j]<<" "<<totalTime<<" "<<runTime<<endl;
// delete []start;
// delete []stop;
//start=NULL;stop=NULL;
}
return 1;
}
int main()
{
TimeSearch(3000);
return 0;
}
楼主用的是vc6?还是VS2008?
VC6.0里,time_t的定义是:
typedef long time_t; /* time value */
其实也就是long,所以可以通用。
但在VS2008中,不一样了:
如何没有定义_USE_32BIT_TIME_T时
typedef __time64_t time_t; /* time value */
typedef __int64 __time64_t; /* 64-bit time value */
也就是说time_t是64位整型的。
在项目属性的"C/C++"中的“预处理定义”中加上_USE_32BIT_TIME_T就可以直接使用
long Time;
了
当然最好把你的代码改为:
struct tm* date_tm;
time_t Time;
time(&Time);
date_tm = localtime(&Time);
int TimeSearch(const long m)
{
int a[1001],n[20];
for(int j=1;j<=1000;j++)a[j]=j;//初始化a
for(int j=0;j<10;j++) //n的取值
{
n[j]=10*j;n[j+10]=100*(j+1);
}
cout<<"n 总时间 运行时间"<<endl;
for(int j=0;j<20;j++)
{
time_t start,stop;
time(&start);//这里要传地址
for(long b=1;b<=m;b++)
{
int k=seqsearch(a,n[j],0);
}
time(&stop);)//这里要传地址
long totalTime=stop-start;
float runTime=(float)(totalTime)/(float)m;
cout<<" "<<n[j]<<" "<<totalTime<<" "<<runTime<<endl;
}
return 1;
}