<script type="text/javascript"></script>
1.如果只是要计算程序运行的时间,不需要那么复杂。
<windows.h> 中的 GetTickCount() 就是干这个的。
TimeStart=GetTickCount();
.......
TimeEnd=GetTickCount();
TimeUsed=TimeEnd-TimeStart;
2. #include<stdio.h>
#include<time.h>
#include<conio.h>
int main()
{
time_t stime , etime ;
time( &stime ); /* get start time */
getch(); /* Access */
time( &etime ); /* get end time */
printf( "%ld\n" , etime - stime );
getch();
return 0;
}
3. class CTimer
{
public:
CTimer() {QueryPerformanceFrequency(&m_Frequency); Start();}
void Start() {QueryPerformanceCounter(&m_StartCount);}
double End() {LARGE_INTEGER CurrentCount;QueryPerformanceCounter(&CurrentCount);return double(CurrentCount.LowPart - m_StartCount.LowPart) / (double)m_Frequency.LowPart;}
private:
LARGE_INTEGER m_Frequency;
LARGE_INTEGER m_StartCount;
};
4. VC的话有profile,在链接属性页勾选profile项,然后profile(在编译菜单下),各个函数时间都出来了
5. #include <iostream>
#include <ctime>
using namespace std;
int max(int x,int y)
{
return (x>y)?x:y;
}
int main()
{
const double begin=(double)clock()/CLK_TCK;
for(int i=10000;i>0;i--)
for(int j=10000;j>0;j--)
max(i,j);
const double end=(double)clock()/CLK_TCK;
cout <<begin<<" "<<end;
return 0;
}
6.要最精确的有
LARGE_INTEGER limtp;
QueryPerformanceFrequency(&limtp);//获得当前的计数频率,即每秒进行多少次计数
QueryPerformanceCounter(&limtp);//获取当前计数次数
基于cpu级的
时间是
(计数获取计数次数 - 开始获取计数次数)/(用QueryPerformanceFrequency获取的limtp.QuadPart)
下面列出简单的例子
#include <ctime> //计时用的头文件
#include <iostream>
using namespace std;
int main()
{
time_t start,end,time; /*注意计时所用的变量名称*/
/*程序开始执行,开始计时*/
start=clock();
/*程序执行过程……*/
for(int i=0;i<=100000;i++) cout << i << ' ';
cout << endl;
/*程序结束执行,结束计时*/
end=clock();
time=end-start;//这里的时间是计算机内部时间
cout << endl << ""time:" << time << endl;
system("pause");
return 0;
}
其它:
Include head file time.h, though it's a C include file, C++ certainly can use it.
Under C++, you can include <ctime> instead of <time.h>
_____________________________________________________
time.h
@函数名称: localtime
函数原型: struct tm *localtime(const time_t *timer)
函数功能: 返回一个以tm结构表达的机器时间信息
函数返回: 以tm结构表达的时间,结构tm定义如下:
struct tm{
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};
参数说明: timer-使用time()函数获得的机器时间
所属文件: <time.h>
#include <time.h>
#include <stdio.h>
#include <dos.h>
int main()
{
time_t timer;
struct tm *tblock;
timer=time(NULL);
tblock=localtime(&timer);
printf("Local time is: %s",asctime(tblock));
return 0;
}
@函数名称: asctime
函数原型: char* asctime(struct tm * ptr)
函数功能: 得到机器时间(日期时间转换为ASCII码)
函数返回: 返回的时间字符串格式为:星期,月,日,小时:分:秒,年
参数说明: 结构指针ptr应通过函数localtime()和gmtime()得到
所属文件: <time.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
int main()
{
struct tm t;
char str[80];
t.tm_sec=1;
t.tm_min=3;
t.tm_hour=7;
t.tm_mday=22;
t.tm_mon=11;
t.tm_year=56;
t.tm_wday=4;
t.tm_yday=0;
t.tm_isdst=0;
strcpy(str,asctime(&t));
printf("%s",str);
return 0;
}
@函数名称: ctime
函数原型: char *ctime(long time)
函数功能: 得到日历时间
函数返回: 返回字符串格式:星期,月,日,小时:分:秒,年
参数说明: time-该参数应由函数time获得
所属文件: <time.h>
#include <stdio.h>
#include <time.h>
int main()
{
time_t t;
time(&t);
printf("Today's date and time: %s",ctime(&t));
return 0;
}
@函数名称: difftime
函数原型: double difftime(time_t time2, time_t time1)
函数功能: 得到两次机器时间差,单位为秒
函数返回: 时间差,单位为秒
参数说明: time1-机器时间一,time2-机器时间二.该参数应使用time函数获得
所属文件: <time.h>
#include <time.h>
#include <stdio.h>
#include <dos.h>
#include <conio.h>
int main()
{
time_t first, second;
clrscr();
first=time(NULL);
delay(2000);
second=time(NULL);
printf("The difference is: %f seconds",difftime(second,first));
getch();
return 0;
}
@函数名称: gmtime
函数原型: struct tm *gmtime(time_t *time)
函数功能: 得到以结构tm表示的时间信息
函数返回: 以结构tm表示的时间信息指针
参数说明: time-用函数time()得到的时间信息
所属文件: <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <dos.h>
char *tzstr="TZ=PST8PDT";
int main()
{
time_t t;
struct tm *gmt, *area;
putenv(tzstr);
tzset();
t=time(NULL);
area=localtime(&t);
printf("Local time is:%s", asctime(area));
gmt=gmtime(&t);
printf("GMT is:%s", asctime(gmt));
return 0;
}
@函数名称: time
函数原型: time_t time(time_t *timer)
函数功能: 得到机器的日历时间或者设置日历时间
函数返回: 机器日历时间
参数说明: timer=NULL时得到机器日历时间,timer=时间数值时,用于设置日历时间,time_t是一个long类型
所属文件: <time.h>
#include <time.h>
#include <stdio.h>
#include <dos.h>
int main()
{
time_t t;
t=time();
printf("The number of seconds since January 1,1970 is %ld",t);
return 0;
}
@函数名称: tzset
函数原型: void tzset(void)
函数功能: UNIX兼容函数,用于得到时区,在DOS环境下无用途
函数返回:
参数说明:
所属文件: <time.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
int main()
{
time_t td;
putenv("TZ=PST8PDT");
tzset();
time(&td);
printf("Current time=%s",asctime(localtime(&td)));
return 0;
}
分享到:
相关推荐
在runtime.h里有个计算程序运行时间的代码,精确到毫秒
这是三种较基本的计算程序运行时间的方法,C++源码实现。
C++获取当前时间和计算程序运行时间的方法
C++语言获取系统时间的工具,有源码及编译后程序 新人第一次上传资源,做的不好,请见谅.
如何写一个C++程序,执行A.exe,计算A.exe的运行时间和内存。
这里总结了C++中的几乎所有时间函数,包括计算程序运行时间的函数,获取当地时间的函数等等.
用于计算C++程序或算法的运行时间,基于C++11.zip
fft程序 c++实现 测试不同长度数据运行时间
解决一个问题通常有多种方法, 我们总想找到最高效的,所以需要对比不同算法执行所用的时间。可惜的是,C++中提供的方法一般只能精确到毫秒级。
功 能: 返回处理器调用某个进程或函数所花费的时间。 用 法: clock_t clock(void);...比如,在VC++6.0下,这两个量的值都是1000,这表示硬件滴答1000下是1秒,因此要计算一个进程的时间,用clock()除以1000即可
是本人的C++课程设计作品,花了一个月时间,运行正常,最后获得优秀。 本项目的任务提出者及开发者是图书销售管理系统软件开发小组,用户是中小型书店。本产品是针对电脑管理书店的需求设计的,可以完成前台销售管理...
举个例子,如果实现同一个功能的两个程序,一个一点按钮给出运行结果,而另一个则需要漫长的时间去等待,像安装WINDOWS XP一样(呵呵,太夸张了吧),你会去使用哪个程序呢?毋庸置疑,优程序的第一条法则是:程序的...
cc实例178 内存使用状态 cc实例179 监视剪贴板内容 cc实例180 利用钩子技术实现键盘监控 6.6 程序相关设置 cc实例181 用列表显示系统正在运行的程序 cc实例182 为程序添加快捷方式 cc实例183 设置其他...
要求使用debug调试功能观察程序运行中变量值的变化情况。 2.定义一个表示时间的结构体,可以精确的表示年、月、日、小时、分、秒;提示用户输入年、月、日、小时、分、秒的值,然后完整地显示出来。 实验二 函数...
cc实例181 用列表显示系统正在运行的程序 cc实例182 为程序添加快捷方式 cc实例183 设置其他程序中编辑框内的文本 cc实例184 执行一个外部程序直到其结束 cc实例185 调用具有参数的可执行...
cc实例181 用列表显示系统正在运行的程序 cc实例182 为程序添加快捷方式 cc实例183 设置其他程序中编辑框内的文本 cc实例184 执行一个外部程序直到其结束 cc实例185 调用具有参数的可执行...
可以计算某一函数或程序运行消耗的时间。记录开始和结束的时间,并算出用时。
c++程序设计这门课程与你们以往所学的数学等不同,有他特有的思考,和解决问题方法,所以一定要掌握这种方法。也许这样说,太空。具体说: (1)一定要化时间,这是前提。你看很多“电脑高手”,都是花很多时间在...