如何用vc 获取系统时间和程序运行时间
c++ 简单获取系统时间

C++获取时间方法有多种,其中比较实用的是localtime函数我们来看下下面这个范例struct tm t; //tm结构指针time_t nowT; //声明time_t类型变量time(&nowT); //获取系统日期和时间t = localtime( &nowT); //获取当地日期和时间printf("%4d年%02d月%02d日%02d:%02d:%02d\n", t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec); //格式化输出本地时间该方法是调用了localtime函数进行时间获取,使用时得包含time.h头文件#include<time.h>但是在vs上不少人会遇到编译器的报错提示,要求把localtime换成localtime_s解释是为了安全性如果出现这种情况我们只要改一行代码即可t = localtime( &nowT); 改为localtime_s( &t,&nowT);如果想要一次性输出日期与时间,那我们就要用到asctime函数struct tm t; //tm结构指针time_t nowT; //声明time_t类型变量time(&nowT); //获取系统日期和时间t = localtime( &nowT); //获取当地日期和时间char TIMET[32];TIMET = asctime(,&t);printf("now is: %s\n",TIMET );如诺出现vs的编译器报错,那只需要改成这样既可struct tm t; //tm结构指针time_t nowT; //声明time_t类型变量time(&nowT); //获取系统日期和时间localtime_s(&t, &nowT); //获取当地日期和时间char TIMET[32];asctime_s(TIMET, &t);printf("now is: %s\n",TIMET);。
C++获取当前系统时间的方法总结

C++获取当前系统时间的⽅法总结本⽂实例讲述了C++获取当前系统时间的⽅法。
分享给⼤家供⼤家参考。
具体如下:⽅案— 优点:仅使⽤C标准库;缺点:只能精确到秒级#include <time.h>#include <stdio.h>int main( void ){time_t t = time(0);char tmp[64];strftime(tmp,sizeof(tmp),"%Y/%m/%d %X %A 本年第%j天 %z",localtime(&t));puts( tmp );return 0;}⽅案⼆优点:能精确到毫秒级;缺点:使⽤了windows API#include <windows.h>#include <stdio.h>int main( void ){SYSTEMTIME sys;GetLocalTime(&sys);printf("%4d/%02d/%02d %02d:%02d:%02d.%03d 星期%1d/n",sys.wYear,sys.wMonth,sys.wDay,sys.wHour,sys.wMinute,sys.wSecond,sys.wMilliseconds,sys.wDayOfWeek); return 0;}⽅案三,优点:利⽤系统函数,还能修改系统时间#include<stdlib.h>#include<iostream>using namespace std;void main(){system("time");}⽅案四,将当前时间折算为秒级,再通过相应的时间换算即可#include<iostream>#include<ctime>using namespace std;int main(){time_t now_time;now_time = time(NULL);cout<<now_time;return 0;}希望本⽂所述对⼤家的C++程序设计有所帮助。
如何获取系统时间与日期

如何获取系统时间与日期1、t m结构在标准C/C++中,我们可通过tm结构来获得日期和时间,tm结构在time.h中的定义如下:#ifndef _TM_DEFINEDstruct tm {int tm_sec; /* 秒–取值区间为[0,59] */int tm_min; /* 分- 取值区间为[0,59] */int tm_hour; /* 时- 取值区间为[0,23] */int tm_mday; /* 一个月中的日期- 取值区间为[1,31] */int tm_mon; /* 月份(从一月开始,0代表一月)- 取值区间为[0,11] */int tm_year; /* 年份,其值等于实际年份减去1900 */int tm_wday; /* 星期–取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推*/int tm_yday; /* 从每年的1月1日开始的天数–取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推*/ int tm_isdst; /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。
不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。
*/};#define _TM_DEFINED#endif2、time_t数据类型日历时间(Calendar Time)是通过time_t数据类型来表示的,用time_t表示的时间(日历时间)是从一个时间点(例如:1970年1月1日0时0分0秒)到此时的秒数。
在time.h中,我们也可以看到time_t是一个长整型数:#ifndef _TIME_T_DEFINEDtypedef long time_t; /* 时间值*/#define _TIME_T_DEFINED /* 避免重复定义time_t */#endif大家可能会产生疑问:既然time_t实际上是长整型,到未来的某一天,从一个时间点(一般是1970年1月1日0时0分0秒)到那时的秒数(即日历时间)超出了长整形所能表示的数的范围怎么办?对time_t数据类型的值来说,它所表示的时间不能晚于2038年1月18日19时14分07秒。
VC++操作注册表与系统时间

基于Visual C++之Windows核心编程代码分析(5)操作注册表与系统时间分类:VC++编程技术Visual C++2010编程技术Visual Studio2012Windows8 2011-12-16 21:33 123人阅读评论(0) 收藏举报我们进行Windows编程的时候,经常需要进行注册表操作,操作注册表请见下列实现代码与说明注释。
view plaincopy to clipboardprint?1./* 头文件 */2.#include <windows.h>3.#include <stdio.h>4.#include <tchar.h>5./* 预定义 */6.#define MAX_KEY_LENGTH 2557.#define MAX_VALUE_NAME 163838./* ************************************9.* void QueryKey(HKEY hKey)10.* 功能列举指定注册表项的子键11.**************************************/12.void QueryKey(HKEY hKey)13.{14.TCHAR achKey[MAX_KEY_LENGTH];15.DWORD cbName;16.TCHAR achClass[MAX_PATH] = TEXT("");17.DWORD cchClassName = MAX_PATH;18.DWORD cSubKeys=0;19.DWORD cbMaxSubKey;20.DWORD cchMaxClass;21.DWORD cValues;22.DWORD cchMaxValue;23.DWORD cbMaxValueData;24.DWORD cbSecurityDescriptor;25. FILETIME ftLastWriteTime;26.27.DWORD i, retCode;28.29.TCHAR achValue[MAX_VALUE_NAME];30.DWORD cchValue = MAX_VALUE_NAME;31.32. // 获取类名和数量33. retCode = RegQueryInfoKey(34. hKey, // 键的句柄35. achClass, // 类名36. &cchClassName, // 类名长度37. NULL, // 保留38. &cSubKeys, // 子键的数量39. &cbMaxSubKey, // 子键长度40. &cchMaxClass, // 类长度41. &cValues, // 子键键值数量42. &cchMaxValue, // 子键名长度43. &cbMaxValueData, // 键值长度44. &cbSecurityDescriptor, // 安全描述符45. &ftLastWriteTime); // 最后写时间46.47. // 列举子键48. if (cSubKeys)49. {50. printf( "\nNumber of subkeys: %d\n", cSubKeys);51.52. for (i=0; i<cSubKeys; i++)53. {54. cbName = MAX_KEY_LENGTH;55. retCode = RegEnumKeyEx(hKey, i,56. achKey,57. &cbName,58. NULL,59. NULL,60. NULL,61. &ftLastWriteTime);62. if (retCode == ERROR_SUCCESS)63. {64. printf(TEXT("(%d) %s\n"), i+1, achKey);65. }66. }67. }68.69. // 列举键值70. if (cValues)71. {72. printf( "\nNumber of values: %d\n", cValues);73.74. for (i=0, retCode=ERROR_SUCCESS; i<cValues; i++)75. {76. cchValue = MAX_VALUE_NAME;77. achValue[0] = '\0';78. retCode = RegEnumValue(hKey, i,79. achValue,80. &cchValue,81. NULL,82. NULL,83. NULL,84. NULL);85.86. if (retCode == ERROR_SUCCESS )87. {88. printf(TEXT("(%d) %s\n"), i+1, achValue);89. }90. }91. }92.}93./* ************************************94.* void AddKey(HKEY hKey)95.* 功能增加一个子键,并设置键值96.**************************************/97.void AddKey(HKEY hKey)98.{99.HKEY hSubKey;100.DWORD dwKeyValue = 100;101. // 创建键102. RegCreateKey(hKey,"MySoftware",&hSubKey); 103. // 设置键值104. if( ERROR_SUCCESS != RegSetValueEx( 105. hSubKey,106. "TEST",107. 0,108. REG_DWORD,109. (LPBYTE)&dwKeyValue,110. sizeof(DWORD)))111. {112. printf("error\n");113. }114.}115./* ************************************116.* void main(void)117.* 功能打开键,获得键句柄118.**************************************/ 119.void main(void)120.{121.HKEY hTestKey;122.123. if( RegOpenKeyEx( HKEY_CURRENT_USER, 124. TEXT("SOFTWARE"),125. 0,126. KEY_READ | KEY_WRITE,127. &hTestKey) == ERROR_SUCCESS128. )129. {130. // 增加键131. AddKey(hTestKey);132. // 列举子键133. QueryKey(hTestKey);134. }135.}1.**************************************/2.#include <Windows.h>3.#include <stdio.h>4./* ************************************5.* int main()6.* 功能获取并显示系统当前时间,然后将时间提前一个小时7.**************************************/8.int main()9.{10. SYSTEMTIME st;11. // 获取当前时间,以本时区时间格式12. GetLocalTime( &st );13. printf("Now: %d-%d-%d, %d:%d:%d",14. st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);15. // 提前一小时16. st.wHour --;17. // 设置当前系统时间18. SetLocalTime( &st );19.}。
c语言计算程序运行时间的函数

c语言计算程序运行时间的函数以C语言计算程序运行时间的函数为题,我们来探讨一下如何在C 语言中编写一个函数来计算程序的运行时间。
在计算机科学中,我们经常需要评估程序的性能,其中一个重要指标就是程序的运行时间。
在C语言中,我们可以使用clock()函数来计算程序的运行时间。
clock()函数返回的是程序运行的时钟周期数,我们可以通过一些操作将其转换为以秒为单位的时间。
我们需要包含<time.h>头文件,这个头文件中包含了clock()函数的定义。
然后,我们可以在程序中定义一个函数来计算程序的运行时间,下面是一个具体的例子:```c#include <stdio.h>#include <time.h>void calculate_time(){clock_t start, end;double cpu_time_used;start = clock(); // 记录开始时间// 在这里插入你的代码,待计算运行时间的代码end = clock(); // 记录结束时间cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC; // 计算运行时间,单位为秒printf("程序运行时间为 %f 秒\n", cpu_time_used);}int main(){calculate_time();return 0;}```在上面的例子中,我们定义了一个名为calculate_time()的函数来计算程序的运行时间。
首先,我们声明了两个clock_t类型的变量start和end,分别用来记录程序的开始时间和结束时间。
然后,我们使用clock()函数来获取当前的时钟周期数,并将其赋值给start 变量。
接着,我们在待计算运行时间的代码前后分别调用了clock()函数,并将返回值赋值给end变量。
最后,我们使用公式((double) (end - start)) / CLOCKS_PER_SEC来计算程序的运行时间,并将结果打印输出。
c和c++在windows下获取时间和计算时间差的方法总结

c/c++在windows下获取时间和计算时间差的几种方法总结一、标准C和C++都可用1、获取时间用time_t time( time_t * timer ),计算时间差使用doubledifftime( time_t timer1, time_t timer0 )。
精确到秒。
测试程序如下:1.#include <time.h>2.#include <stdio.h>3.int main()4.{5. time_t start ,end ;6.double cost;7. time(&start);8. sleep(1);9. time(&end);10. cost=difftime(end,start);11. printf("%f\n",cost);12.return 0;13.}本程序在fedora9测试通过。
关于代码中的sleep函数,需要注意的是:1)在windows下,为Sleep()函数,且需包含windows.h2)关于sleep中的数,在Windows和Linux下1000代表的含义并不相同,Windows 下的表示1000毫秒,也就是1秒钟;Linux下表示1000秒,Linux下使用毫秒级别的函数可以使用usleep。
2、clock_t clock()clock() 获取的是计算机启动后的时间间隔,得到的是CPU时间,精确到1/CLOCKS_PER_SEC秒。
测试程序如下:1.#include <time.h>2.#include <stdio.h>3.int main()4.{5.double start,end,cost;6. start=clock();7. sleep(1);8. end=clock();9. cost=end-start;10. printf("%f\n",cost);11.return 0;12.}二、C++中(此处针对windows环境,标准c中则linux和windows都可以)1、GetTickCount()调用函数需包含windows.h。
C语言计算程序运行时间

C语言计算程序运行时间clock(函数返回的是一个CLOCKS_PER_SEC宏定义的时钟周期数,表示每秒钟的时钟周期数。
通过使用clock(函数,我们可以获取程序运行的起始时间和结束时间,然后计算两者之间的差值,即可得到程序的运行时间。
下面是一个使用clock(函数计算程序运行时间的示例代码:```c#include <stdio.h>int maiclock_t start, end;start = clock(; // 获取程序开始时间//以下是程序的主要代码int sum = 0;sum += i;}//程序主要代码结束end = clock(; // 获取程序结束时间return 0;```在上面的示例代码中,我们首先声明了两个clock_t类型的变量start和end,用于存储程序开始和结束的时钟时间。
然后,在程序执行前使用clock(函数获取程序开始的时钟时间,并将其赋值给start变量。
接着,在程序的主要代码部分,我们进行了一些耗时的操作(本例中是一个求和操作),这部分代码是我们希望计算运行时间的代码。
最后,在程序主要代码结束后使用clock(函数获取程序结束的时钟时间,并将其赋值给end变量。
通过计算end和start之间的差值,并除以CLOCKS_PER_SEC,我们可以得到程序运行的总时间(以秒为单位)。
最后,我们使用printf函数将运行时间打印出来。
在上述示例中,使用了整数求和的操作作为耗时的示例代码,你可以根据需要替换为实际的耗时操作。
使用clock(函数计算程序运行时间的原理是一样的,只需要在程序的开始和结束处添加相应的代码即可。
cc++测试函数的运行时间(八种方法)

cc++测试函数的运⾏时间(⼋种⽅法)⽬前,存在着各种计时函数,⼀般的处理都是先调⽤计时函数,记下当前时间tstart,然后处理⼀段程序,再调⽤计时函数,记下处理后的时间tend,再tend和tstart做差,就可以得到程序的执⾏时间,但是各种计时函数的精度不⼀样.下⾯对各种计时函数,做些简单记录.void foo(){long i;for (i=0;i<100000000;i++){long a= 0;a = a+1;}}⽅法1,time()获取当前的系统时间,返回的结果是⼀个time_t类型,其实就是⼀个⼤整数,其值表⽰从CUT(Coordinated Universal Time)时间1970年1⽉1⽇00:00:00(称为UNIX系统的Epoch时间)到当前时刻的秒数.void test1(){time_t start,stop;start = time(NULL);foo();//dosomethingstop = time(NULL);printf("Use Time:%ld\n",(stop-start));}⽅法2,clock()函数返回从“开启这个程序进程”到“程序中调⽤clock()函数”时之间的CPU时钟计时单元(clock tick)数,在MSDN中称之为挂钟时间(wal-clock)常量CLOCKS_PER_SEC,它⽤来表⽰⼀秒钟会有多少个时钟计时单元。
void test2(){double dur;clock_t start,end;start = clock();foo();//dosomethingend = clock();dur = (double)(end - start);printf("Use Time:%f\n",(dur/CLOCKS_PER_SEC));}如果你想学习C/C++可以来这个群,⾸先是三三零,中间是⼋五九,最后是七六六,⾥⾯有⼤量的学习资料可以下载。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
标题:如何用vc++获取系统时间和程序运行时间 出处:春天的事业 时间:Mon, 22 Jun 2009 17:34:26 +0000 作者:xiechunye 地址:http://www.xiechunye.cn/read.php/612.htm
内容: Q:如何获取时间?精度如何? A: 1 使用time_t time( time_t * timer ) 精确到秒 计算时间差使用double difftime( time_t timer1, time_t timer0 ) 2 使用clock_t clock() 得到的是CPU时间 精确到1/CLOCKS_PER_SEC秒 3 使用DWORD GetTickCount() 得到的是系统运行的时间 精确到毫秒 4 如果使用MFC的CTime类,可以用CTime::GetCurrentTime() 精确到秒 5 要获取高精度时间,可以使用 BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency)获取系统的计数器的频率 BOOL QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount)获取计数器的值 然后用两次计数器的差除以Frequency就得到时间。 6 还有David的文章中提到的方法: Multimedia Timer Functions The following functions are used with multimedia timers. timeBeginPeriod/timeEndPeriod/timeGetDevCaps/timeGetSystemTime timeGetTime/timeKillEvent/TimeProc/timeSetEvent 精度很高 Q:GetTickCount()函数,说是毫秒记数,是真的吗,还是精确到55毫秒? A: GetTickCount()和GetCurrentTime()都只精确到55ms(1个tick就是55ms)。如果要精确到毫秒,应该使用timeGetTime函数或QueryPerformanceCounter函数。具体例子可以参考QA001022 "VC++中使用高精度定时器"、QA001813 "如何在Windows实现准确的定时"和QA004842 "timeGetTime函数延时不准"。 Q:vc++怎样获取系统时间,返回值是什么类型的变量呢? GetSystemTime返回的是格林威志标准时间 GetLocalTime,和上面用法一样,返回的是你所在地区的时间,中国返回的是北京时间 VOID GetSystemTime( LPSYSTEMTIME lpSystemTime // address of system time structure ); 函数就可以获得了,其中LPSYSTEMTIME 是个结构体 含:年,月,日,周几,小时,分,秒,毫秒。 以下是Time的MSDN文档: Compatibility in the Introduction. Libraries LIBC.LIBSingle thread static library, retail versionLIBCMT.LIBMultithread static library, retail versionMSVCRT.LIBImport library for MSVCRT.DLL, retail version Return Value time returns the time in elapsed seconds. There is no error return. Parameter timer Storage location for time Remarks The time function returns the number of seconds elapsed since midnight (00:00:00), January 1, 1970, coordinated universal time, according to the system clock. The return value is stored in the location given by timer. This parameter may be NULL, in which case the return value is not stored. Example /* TIMES.C illustrates various time and date functions including: * time _ftime ctime asctime * localtime gmtime mktime _tzset * _strtime _strdate strftime * * Also the global variable: * _tzname */ #include #include #include #include #include void main() { char tmpbuf[128], ampm[] = "AM"; time_t ltime; struct _timeb tstruct; struct tm *today, *gmt, xmas = { 0, 0, 12, 25, 11, 93 }; /* Set time zone from TZ environment variable. If TZ is not set, * the operating system is queried to obtain the default value * for the variable. */ _tzset(); /* Display operating system-style date and time. */ _strtime( tmpbuf ); printf( "OS time:\t\t\t\t%s\n", tmpbuf ); _strdate( tmpbuf ); printf( "OS date:\t\t\t\t%s\n", tmpbuf ); /* Get UNIX-style time and display as number and string. */ time( <ime ); printf( "Time in seconds since UTC 1/1/70:\t%ld\n", ltime ); printf( "UNIX time and date:\t\t\t%s", ctime( <ime ) ); /* Display UTC. */ gmt = gmtime( <ime ); printf( "Coordinated universal time:\t\t%s", asctime( gmt ) ); /* Convert to time structure and adjust for PM if necessary. */ today = localtime( <ime ); if( today->tm_hour > 12 ) { strcpy( ampm, "PM" ); today->tm_hour -= 12; } if( today->tm_hour == 0 ) /* Adjust if midnight hour. */ today->tm_hour = 12; /* Note how pointer addition is used to skip the first 11 * characters and printf is used to trim off terminating * characters. */ printf( "12-hour time:\t\t\t\t%.8s %s\n", asctime( today ) + 11, ampm ); /* Print additional time information. */ _ftime( &tstruct ); printf( "Plus milliseconds:\t\t\t%u\n", tstruct.millitm ); printf( "Zone difference in seconds from UTC:\t%u\n", tstruct.timezone ); printf( "Time zone name:\t\t\t\t%s\n", _tzname[0] ); printf( "Daylight savings:\t\t\t%s\n", tstruct.dstflag ? "YES" : "NO" ); /* Make time for noon on Christmas, 1993. */ if( mktime( &xmas ) != (time_t)-1 ) printf( "Christmas\t\t\t\t%s\n", asctime( &xmas ) ); /* Use time structure to build a customized time string. */ today = localtime( <ime ); /* Use strftime to build a customized time string. */ strftime( tmpbuf, 128, "Today is %A, day %d of %B in the year %Y.\n", today ); printf( tmpbuf ); }
Output OS time: 21:51:03 OS date: 05/03/94 Time in seconds since UTC 1/1/70: 768027063 UNIX time and date: Tue May 03 21:51:03 1994 Coordinated universal time: Wed May 04 04:51:03 1994 12-hour time: 09:51:03 PM