C++中如何记录程序运行时间的相关代码间

C++中计算程序运行时间
[问题点数:25分,结帖人wood2015fire]
本版专家分:0
结帖率 100%
CSDN今日推荐
本版专家分:0
本版专家分:0
本版专家分:0
匿名用户不能发表回复!
其他相关推荐C++测试程序运行时间的代码_Linux编程_Linux公社-Linux系统门户网站
你好,游客
C++测试程序运行时间的代码
来源:C++&
FIRST CODE:#include&iostream&#include&ctime&int main(){time_t begin,begin=clock();//your codeend=clock();cout&&"runtime: "&&double(end-begin)/CLOCKS_PER_SEC&&}
DECOND CODE:#include &iostream&&&&& #include &windows.h&&&&& &&&& && void Test()//测试程序&&& {&&& &&& for(int i=0; i&10; i++)&&& &&& {&&&&&& &&&&&&& for(int j=0; j&10; j++)&&& &&&&&&& {&&& &&&&&&&&&&& printf("%d,%d\n",i,j);&&& &&&&&&& }&&&&&& &&& }&&& }&&& && int main(void)&&&& {&&&& &&& LARGE_INTEGER BegainT&&&& &&& LARGE_INTEGER EndT&&&& &&& LARGE_INTEGER F&&&& &&& QueryPerformanceFrequency(&Frequency);&&&& &&& QueryPerformanceCounter(&BegainTime) ;&&&& && &&& //要测试的代码放在这里&&&& &&& Test();&&& && &&& QueryPerformanceCounter(&EndTime);&&& && &&& //输出运行时间(单位:s)&&&& &&& cout && "运行时间(单位:s):" &&(double)( EndTime.QuadPart - BegainTime.QuadPart )/ Frequency.QuadPart &&&&&& && &&& system("pause") ;&&&& &&& return 0 ;&&&& }&&&
相关资讯 & & &
& (03月17日)
& (05/23/:27)
& (05月21日)
& (03月12日)
& (05/22/:16)
   同意评论声明
   发表
尊重网上道德,遵守中华人民共和国的各项有关法律法规
承担一切因您的行为而直接或间接导致的民事或刑事法律责任
本站管理人员有权保留或删除其管辖留言中的任意内容
本站有权在网站内转载或引用您的评论
参与本评论即表明您已经阅读并接受上述条款
pys516 发表于 这一个程序真好!DWORD start_time=GetTickCount();
DWORD end_time=GetTickCount();
DWORD Subtime = (end_time-start_time);
int k = 0;
如何获取代码运行时间
在调试中,经常需要计算某一段代码的执行时间,下面给出两种常用的方式:
第一种:使用GetTickCount函数
#include&iostream&
#include&windows.h&
int main()
DWORD start_time=GetTickCount();
//此处为被测试代码
DWORD end_time=GetTickCount();
cout&&"The run time is:"&&(end_time-start_time)&&"ms!"&&//输出运行时间
} GetTickCount函数返回从系统运行到现在所经历的时间(类型为DWORD),单位为ms,因为DWORD表示范围的限制,所以使用此种方法存在限制,即系统的运行时间的ms表示不能超出
DWORD的表示范围。
第二种:使用clock()函数
#include&iostream&
#include&time.h&
int main()
clock_t start_time=clock();
//被测试代码
clock_t end_time=clock();
cout&& "Running time is: "&&static_cast&double&(end_time-start_time)/CLOCKS_PER_SEC*1000&&"ms"&&//输出运行时间
} clock_t,clock()定义于time.h中,clock()返回从程序运行时刻开始的时钟周期数,类型为long.CLOCKS_PER_SEC定义了每秒钟包含多少了时钟单元数,因为计算ms,所以
由上面分析可知,用clock()函数计算运行时间,表示范围一定大于GetTickCount()函数,所以,建议使用clock()函数。
===========================================================================================================
1.使用CTime类
//获取系统时间
tm=CTime::GetCurrentTime();
str=tm.Format("现在时间是%Y年%m月%d日 %X");
MessageBox(str,NULL,MB_OK);
2: 得到系统时间日期(使用GetLocalTime)
SYSTEMTIME
CString strDate,strT
GetLocalTime(&st);
strDate.Format("%4d-%2d-%2d",st.wYear,st.wMonth,st.wDay);
strTime.Format("%2d:%2d:%2d",st.wHour,st.wMinute,st.wSecond);
3.使用GetTickCount
//获取程序运行时间
long t1=GetTickCount();//程序段开始前取得系统运行时间(ms)
Sleep(500);
long t2=GetTickCount();();//程序段结束后取得系统运行时间(ms)
str.Format("time:%dms",t2-t1);//前后之差即 程序运行时间
AfxMessageBox(str);
//获取系统运行时间
long t=GetTickCount();
CString str,str1;
str1.Format("系统已运行 %d时",t/3600000);
t%=3600000;
str1.Format("%d分",t/60000);
str+=str1;
str1.Format("%d秒",t/1000);
str+=str1;
AfxMessageBox(str);
如何在VC6.0中得到一个程序的运行时间,也就是这个程序耗费的时钟周期数// C和C++的时间编程
#include&iostream&
#include&ctime&
int main()
time_t begin,
begin=clock();
//这里加上你的代码
end=clock();
cout&&"runtime: "&&double(end-begin)/CLOCKS_PER_SEC&&
unix时间相关,也是标准库的
这些在&time.h&
1.timegm函数只是将struct tm结构转成time_t结构,不使用时区信息;
time_t timegm(struct tm *tm);
2.mktime使用时区信息
time_t mktime(struct tm *tm);
timelocal 函数是GNU扩展的与posix函数mktime相当
time_t timelocal (struct tm *tm);
3.gmtime函数只是将time_t结构转成struct tm结构,不使用时区信息;
struct tm * gmtime(const time_t *clock);
4.localtime使用时区信息
struct tm * localtime(const time_t *clock);
1.time获取时间,stime设置时间
time_t t;
t = time(&t);
2.stime其参数应该是GMT时间,根据本地时区设置为本地时间;
int stime(time_t *tp)
3.UTC=true 表示采用夏时制;
4.文件的修改时间等信息全部采用GMT时间存放,不同的系统在得到修改时间后通过localtime转换成本地时间;
5.设置时区推荐使用setup来设置;
6.设置时区也可以先更变/etc/sysconfig/clock中的设置 再将ln -fs /usr/share/zoneinfo/xxxx/xxx /etc/localtime 才能重效
time_t只能表示68年的范围,即mktime只能返回这一段范围的time_t
看看你的系统是否有time_t64,它能表示更大的时间范围
Window里面的一些不一样的
CTime MFC类,好像就是把time.h封了个类,没扩展
CTime t = GetCurrentTime();
SYSTEMTIME 结构包含毫秒信息
typedef struct _SYSTEMTIME {
WORD wDayOfW
} SYSTEMTIME, *PSYSTEMTIME;
SYSTEMTIME t1;
GetSystemTime(&t1)
CTime curTime(t1);
WORD ms = t1.wM
SYSTEMTIME sysTm;
::GetLocalTime(&sysTm);
在time.h中的_strtime() //只能在windows中用
char t[11];
_strtime(t);
------------------------------------------------------------------------------
_timeb定义在SYS\TIMEB.H,有四个fields
void _ftime( struct _timeb *timeptr );
_ftime( &timebuffer );
取当前时间:文档讲可以到ms,有人测试,好象只能到16ms!
-------------------------------------------------------------------------
如何设定当前系统时间---windows
SYSTEMTIME m_myLocalTime,*lpSystemT
m_myLocalTime.wYear=2003;
m_myLocalTime.wMonth=1;
m_myLocalTime.wDay=1;
m_myLocalTime.wHour=0;
m_myLocalTime.wMinute=0;
m_myLocalTime.wSecond=0;
m_myLocalTime.wMilliseconds=0;
lpSystemTime=&m_myLocalT
if( SetLocalTime(lpSystemTime) ) //此处换成 SetSystemTime( )也不行
MessageBox("OK !");
MessageBox("Error !");
SYSTEMTIME m_myLocalTime,*lpSystemT
m_myLocalTime.wYear=2003;
m_myLocalTime.wMonth=1;
m_myLocalTime.wDay=1;
lpSystemTime=&m_myLocalT
if( SetDate(lpSystemTime) ) //此处换成 SetSystemTime( )也不行
MessageBox("OK !");
MessageBox("Error !");
-----------------------------------------------------------------------------
用clock()函数,得到系统启动以后的毫秒级时间,然后除以CLOCKS_PER_SEC,就可以换成“秒”,标准c函数。
clock_t clock ( void );
#include &time.h&
clock_t t = clock();
long sec = t / CLOCKS_PER_SEC;
他是记录时钟周期的,实现看来不会很精确,需要试验验证;
---------------------------------------------------------------------------
据说tc2.0的time结构含有毫秒信息
#include &stdio.h&
#include &dos.h&
int main(void)
gettime(&t);
printf("The current time is: %2d:%02d:%02d.%02d\n",
t.ti_hour, t.ti_min, t.ti_sec, t.ti_hund);
time 是一个结构体,, 其中成员函数 ti_hund 是豪秒。。。上程序可以在tc2.0运行
--------------------------------------------------------------------------------
这个是windows里面常用来计算程序运行时间的函数;
DWORD dwStart = GetTickCount();
//这里运行你的程序代码
DWORD dwEnd = GetTickCount();
则(dwEnd-dwStart)就是你的程序运行时间, 以毫秒为单位
这个函数只精确到55ms,1个tick就是55ms。
--------------------------------------------------------------------------------
timeGetTime()基本等于GetTickCount(),但是精度更高
DWORD dwStart = timeGetTime();
//这里运行你的程序代码
DWORD dwEnd = timeGetTime();
则(dwEnd-dwStart)就是你的程序运行时间, 以毫秒为单位
虽然返回的值单位应该是ms,但传说精度只有10ms。
--------------------------------------------------------------------------------
c/c++测试函数的运行时间(八种方法)(转)
目前,存在着各种计时函数,一般的处理都是先调用计时函数,记下当前时间tstart,然后处理一段程序,再调用计时函数,记下处理后的时间tend,再tend和tstart做差,就可以得到程序的执行时间,但...
C++中两种常用的记录程序运行时间的方法的总结
本篇博客对C++中常用的记录程序运行时间的方法进行简单的总结。
C++程序运行时间计算
转载自:http://blog.163.com/f_jinyi/blog/static//
1.如果只是要计算程序运行的时间,不需要那么复杂。
【C++】C++中几种测试程序运行时间的方法
关于C++中计算时间的一些总结
一、使用GetTickCount()函数
GetTickCount()是一个函数,可以把它理解为打点计时器。GetTickCount()是通过计算从函数开始运行计时...
C/C++获取程序执行时间的五个方法对比
五种获取C/C++程序执行时间的方法对比如下:
测试C++程序运行的时间
参加一个公司的笔试,给了两个题目,时间要求是越快越好,这时候就要求我们在编写程序之后测试程序所需的时间,尽量做到最小。
在C++中计算运行的时间是调用clock函数,使用clock函数获得程序开始和...
Linux C/C++获取程序运行时间
获取当前时间戳数据,程序中不同位置时间数据做差即可得到程序运行时间。1 实现基础: int gettimeofday(struct timeval *tv, struct timezone *tz);...
C++获取当前时间和计算程序运行时间的方法
获取当前时间:
int main()
SYSTEMTIME
GetLocalTime(...
C++记录程序运行时间
int main(int argc, char const *argv[])
C++获得一段代码的运行时间
#define _CRT_SECURE_NO_WARNINGS
//基于vs2008下的
没有更多推荐了,找不到答案?
在C++程序中如何实现查看正在运行的其他程序
在C++程序中如何实现查看正在运行的其他程序
按投票排序
按时间排序
<dd id="vote_num_
void _FindProcess(const CString& strProcessName,CArray&DWORD,DWORD&& aPid)
aPid.RemoveAll();
DWORD dwPid = 0; //用于保存Id
CString strExeF
HANDLE hHandle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); //为当前系统进程建立快照
DWORD dwId = ::GetCurrentProcessId(); //当前进程的Id
if (INVALID_HANDLE_VALUE !=hHandle) //如果快照建立成功
PROCESSENTRY32 stE
stEntry.dwSize = sizeof(PROCESSENTRY32);
if(Process32First(hHandle, &stEntry)) //在快照中查找一个进程,stEntry返回进程相关属性和信息
strExeFile.Format("%s",stEntry.szExeFile);
if(strProcessName.CompareNoCase(strExeFile)==0) //比较该进程名称是否与strProcessName相符
if(dwId != stEntry.th32ProcessID) //如果相等,且该进程的Id与当前进程不相等,则找到strProcessName对应的进程。
aPid.Add(stEntry.th32ProcessID);
}while(Process32Next(hHandle, &stEntry));//再快照中查找下一个进程。
CloseHandle(hHandle); //释放快照句柄。
上面是查找进程名.你稍修改一下就可以用了.满意请采纳
<dd id="vote_num_
//code form MSDN
#include &iostream&
#include &windows.h&
#include &tlhelp32.h&
#include &tchar.h&
Forward declarations:
BOOL GetProcessList( );
BOOL ListProcessModules( DWORD dwPID );
BOOL ListProcessThreads( DWORD dwOwnerPID );
void printError( TCHAR* msg );
int main( void )
GetProcessList( );
BOOL GetProcessList( )
HANDLE hProcessS
PROCESSENTRY32 pe32;
DWORD dwPriorityC
// Take a snapshot of all processes in the system.
hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
if( hProcessSnap == INVALID_HANDLE_VALUE )
printError( TEXT("CreateToolhelp32Snapshot (of processes)") );
return( FALSE );
// Set the size of the structure before using it.
pe32.dwSize = sizeof( PROCESSENTRY32 );
// Retrieve information about the first process,
// and exit if unsuccessful
if( !Process32First( hProcessSnap, &pe32 ) )
printError( TEXT("Process32First") ); // show cause of failure
CloseHandle( hProcessSnap );
// clean the snapshot object
return( FALSE );
// Now walk the snapshot of processes, and
// display information about each process in turn
//_tprintf( TEXT("nn=====================================================" ));
//_tprintf( TEXT("nPROCESS NAME:
%s"), pe32.szExeFile );
//_tprintf( TEXT("n-------------------------------------------------------" ));
cout&&"nn====================================================="&&
cout&&"PROCESS NAME:"&&pe32.szExeFile&&
cout&&"n-------------------------------------------------------"&&
// Retrieve the priority class.
dwPriorityClass = 0;
hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID );
if( hProcess == NULL )
printError( TEXT("OpenProcess") );
dwPriorityClass = GetPriorityClass( hProcess );
if( !dwPriorityClass )
printError( TEXT("GetPriorityClass") );
CloseHandle( hProcess );
_tprintf( TEXT("n
Process ID
= 0x%08X"), pe32.th32ProcessID );
_tprintf( TEXT("n
Thread count
pe32.cntThreads );
_tprintf( TEXT("n
Parent process ID = 0x%08X"), pe32.th32ParentProcessID );
_tprintf( TEXT("n
Priority base
= %d"), pe32.pcPriClassBase );
if( dwPriorityClass )
_tprintf( TEXT("n
Priority class
= %d"), dwPriorityClass );
// List the modules and threads associated with this process
ListProcessModules( pe32.th32ProcessID );
ListProcessThreads( pe32.th32ProcessID );
} while( Process32Next( hProcessSnap, &pe32 ) );
CloseHandle( hProcessSnap );
return( TRUE );
BOOL ListProcessModules( DWORD dwPID )
HANDLE hModuleSnap = INVALID_HANDLE_VALUE;
MODULEENTRY32 me32;
// Take a snapshot of all modules in the specified process.
hModuleSnap = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, dwPID );
if( hModuleSnap == INVALID_HANDLE_VALUE )
printError( TEXT("CreateToolhelp32Snapshot (of modules)") );
return( FALSE );
// Set the size of the structure before using it.
me32.dwSize = sizeof( MODULEENTRY32 );
// Retrieve information about the first module,
// and exit if unsuccessful
if( !Module32First( hModuleSnap, &me32 ) )
printError( TEXT("Module32First") );
// show cause of failure
CloseHandle( hModuleSnap );
// clean the snapshot object
return( FALSE );
// Now walk the module list of the process,
// and display information about each module
_tprintf( TEXT("nn
MODULE NAME:
me32.szModule );
_tprintf( TEXT("n
Executable
me32.szExePath );
_tprintf( TEXT("n
Process ID
= 0x%08X"),
me32.th32ProcessID );
_tprintf( TEXT("n
Ref count (g)
= 0x%04X"),
me32.GlblcntUsage );
_tprintf( TEXT("n
Ref count (p)
= 0x%04X"),
me32.ProccntUsage );
_tprintf( TEXT("n
Base address
= 0x%08X"), (DWORD) me32.modBaseAddr );
_tprintf( TEXT("n
me32.modBaseSize );
} while( Module32Next( hModuleSnap, &me32 ) );
CloseHandle( hModuleSnap );
return( TRUE );
BOOL ListProcessThreads( DWORD dwOwnerPID )
HANDLE hThreadSnap = INVALID_HANDLE_VALUE;
THREADENTRY32 te32;
// Take a snapshot of all running threads
hThreadSnap = CreateToolhelp32Snapshot( TH32CS_SNAPTHREAD, 0 );
if( hThreadSnap == INVALID_HANDLE_VALUE )
return( FALSE );
// Fill in the size of the structure before using it.
te32.dwSize = sizeof(THREADENTRY32);
// Retrieve information about the first thread,
// and exit if unsuccessful
if( !Thread32First( hThreadSnap, &te32 ) )
printError( TEXT("Thread32First") ); // show cause of failure
CloseHandle( hThreadSnap );
// clean the snapshot object
return( FALSE );
// Now walk the thread list of the system,
// and display information about each thread
// associated with the specified process
if( te32.th32OwnerProcessID == dwOwnerPID )
_tprintf( TEXT("nn
= 0x%08X"), te32.th32ThreadID );
_tprintf( TEXT("n
Base priority
= %d"), te32.tpBasePri );
_tprintf( TEXT("n
Delta priority = %d"), te32.tpDeltaPri );
_tprintf( TEXT("n"));
} while( Thread32Next(hThreadSnap, &te32 ) );
CloseHandle( hThreadSnap );
return( TRUE );
void printError( TCHAR* msg )
TCHAR sysMsg[256];
eNum = GetLastError( );
FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, eNum,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
sysMsg, 256, NULL );
// Trim the end of the line and terminate it with a null
while( ( *p & 31 ) || ( *p == 9 ) )
do { *p-- = 0; } while( ( p &= sysMsg ) &&
( ( *p == '.' ) || ( *p & 33 ) ) );
// Display the message
_tprintf( TEXT("n
WARNING: %s failed with error %d (%s)"), msg, eNum, sysMsg );
登录后可回答问题,请
为您提供简单高效、处理能力可弹性伸缩的计算服务,帮助您快速构建更稳定、安全的应用,提升运维效率,降低 IT 成本...
RDS是一种稳定可靠、可弹性伸缩的在线数据库服务。支持MySQL、SQL Server、PostgreSQL、高...如何统计C++程序运行时间?_百度知道
如何统计C++程序运行时间?
如题,我编写了一个c++程序,我想统计下这个程序得运行时间,请问如何可以加入一段代码,当我运行的时候它自动开始统计时间?...
如题,我编写了一个c++程序,我想统计下这个程序得运行时间,请问如何可以加入一段代码,当我运行的时候它自动开始统计时间?
&#xe6b9;答题抽奖
首次认真答题后
即可获得3次抽奖机会,100%中奖。
一生何求725
一生何求725
采纳数:723
获赞数:1331
1、使用clock函数获得程序开始和结束的时间,相减就能得到程序运行的时间。clock()是C/C++中的计时函数,而与其相关的数据类型是clock_t。在MSDN中,查得对clock函数定义如下:clock_t clock(void) ;简单而言,就是该程序从启动到函数调用占用CPU的时间。这个函数返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元(clock tick)数,在MSDN中称之为挂钟时间(wal-clock);若挂钟时间不可取,则返回-1。其中clock_t是用来保存时间的数据类型。例程:#include&iostream&#include&ctime& int main(){
clock_t start,
start=clock();
cout && &HW .... & &&
finish=clock();
cout && finish-start
&& &/& && CLOCKS_PER_SEC
&& & (s) &&&
return 0;}3、转
#include &time.h&#include &stdio.h&void main(){ time_t t1; time(&t1); for(int i=0;i&;i++) {
int a=i; }time_t t2; time(&t2); printf(&已运行%d秒&#92;n&,t2-t1);}
本回答被网友采纳
long clock(void); 返回程序开始执行后占用的处理器时间。
采纳数:64
获赞数:103
擅长:暂未定制
如果代码还需要运行别的功能。需要使用进程。 不需要的话使用sleep函数休眠计时就可以了
其他2条回答
为你推荐:
其他类似问题
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。

我要回帖

更多关于 怎样查看ipad运行程序记录 的文章

 

随机推荐