数据结构里面性能测量的例子。。time函数怎么实现的啊?。。我查MSDN包含了time.h头文件

aweer 2009-03-30 11:54:00
我查了MSDN是time.h头文件,是接受time_t的数据类型参数的。。但是书上的例子却是用long来接受的。。。谁能帮忙改下啊谢谢了~~书上的例子:
#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;
}
...全文
314 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
mengde007 2009-03-30
  • 打赏
  • 举报
回复

#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;
}


chenzhp 2009-03-30
  • 打赏
  • 举报
回复
楼主用的是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);
aweer 2009-03-30
  • 打赏
  • 举报
回复
但是我包含了TIME.h 编译器也给个这样的错误
1>d:\c++primer\test\binary\binary\main.cpp(24) : error C2664: 'time' : cannot convert parameter 1 from 'long' to 'time_t *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
Dinelgua 2009-03-30
  • 打赏
  • 举报
回复
MSDN
time_t long integer Represents time values in mktime and time. TIME.H

#ifndef _TIME_T_DEFINED
#ifdef _WIN64
typedef __int64 time_t; /* time value */
#else
typedef long time_t; /* time value */
#endif

所以 time_t 32为下就是 long 换个名字而已
nnliubin 2009-03-30
  • 打赏
  • 举报
回复
其实这个测时间的方法很简单
time函数原型为time_t time(time_t * timer);
你可以直接time(&start)获得 也可以start=time(NULL)获得
(值为long型表示标准时间点距离现在的秒数)
其实我个人觉得只是单单测时间的话用clock()函数比较方便
比如
#include<iostream>
#include<time.h>

using namespace std;

int main()
{

clock_t start, finish;
int sum=0;
double duration;


start=clock();

for(int i=0; i<100000000; i++)
{
sum+=i;
}

finish=clock();

duration=double(finish-start);

cout<<duration<<endl;//单位毫秒
return 0;
}

Dinelgua 2009-03-30
  • 打赏
  • 举报
回复
问题找到了 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;
}
aweer 2009-03-30
  • 打赏
  • 举报
回复
4楼的也不行啊
aweer 2009-03-30
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 chenzhp 的回复:]
C/C++ code楼主用的是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…
[/Quote]



按照你的加上了还是不行。。一样的错误

64,652

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧