unix / linux 下,使用 time 命令 + 程序名,可以跟踪程序的 运行时间( 实际运行时间,用户空间执行时间,系统空间执行时间。
要跟踪某个函数花了多长时间,就需要自己动手来实现了。
使用宏可以轻松地搞定这个事情。思想是:在函数执行前后各取一个系统当前时间。并打印出来。
下面是代码,可精确到微秒。
/*
timechk.h
下面的宏用来测试某个函数的运行时间
用于 linux/unix 平台
使用中有任何问题,欢迎交流 p_168@163.com
我的博客: http://diylab.cnblogs.com
*/
#include <sys/time.h>
#include <stdio.h>
#ifndef _TIMECHK_
#define checktime( fun )
#else
#define checktime( fun ) { \
int diffsec, diffusec; \
struct timeval tstart, tend; \
gettimeofday( &tstart, 0 ); \
fun; \
gettimeofday( &tend, 0 ); \
diffusec = tend.tv_usec - tstart.tv_usec ; \
diffsec = tend.tv_sec - tstart.tv_sec ; \
if ( diffusec < 0 ) \
{ \
diffsec -=1; \
diffusec +=1000000; \
} \
#endif
timechk.h
下面的宏用来测试某个函数的运行时间
用于 linux/unix 平台
使用中有任何问题,欢迎交流 p_168@163.com
我的博客: http://diylab.cnblogs.com
*/
#include <sys/time.h>
#include <stdio.h>
#ifndef _TIMECHK_
#define checktime( fun )
#else
#define checktime( fun ) { \
int diffsec, diffusec; \
struct timeval tstart, tend; \
gettimeofday( &tstart, 0 ); \
fun; \
gettimeofday( &tend, 0 ); \
diffusec = tend.tv_usec - tstart.tv_usec ; \
diffsec = tend.tv_sec - tstart.tv_sec ; \
if ( diffusec < 0 ) \
{ \
diffsec -=1; \
diffusec +=1000000; \
} \
char buf[32] = { 0 }; \
strncpy( buf, sizeof( buf ) - 1, #fun ); \
printf( "%s: %d.%03ds\n", buf, diffsec, diffusec );\
};strncpy( buf, sizeof( buf ) - 1, #fun ); \
printf( "%s: %d.%03ds\n", buf, diffsec, diffusec );\
#endif
下面是测试程序:
// 测试源码 main.c
#define _TIMECHK_
#include "timechk.h"
int myfun( int i )
{
printf( "i=%d\n", i );
sleep(2);
return 0;
}
int main()
{
checktime( myfun(2) );
return 0;
}
也附上 windows 版本,只是示例,不是太严格。
/// windows 版本
/// vc6.0 没有带 windows.h 所以测试时需要 vc7以上,或 安装了 windddk
#include <windows.h>
#define checktime( fun ) { \
int diffsec, diffusec; \
SYSTEMTIME tstart, tend; \
FILETIME tfilestart, tfileend;\
GetSystemTime( &tstart ); \
fun; \
GetSystemTime( &tend ); \
SystemTimeToFileTime( &tstart , &tfilestart); \
diffusec = tend.wMilliseconds - tstart.wMilliseconds ; \
diffsec = tend.wSecond - tstart.wSecond ; \
if ( diffusec < 0 ) \
{ \
diffsec -=1; \
diffusec +=1000; \
} \
int myfun( int i )
{
printf( "i=%d\n", i );
Sleep(2123);
return 0;
}
int main(int argc, char* argv[])
{
checktime( myfun(2) );
system( "pause" );
return 0;
}
/// vc6.0 没有带 windows.h 所以测试时需要 vc7以上,或 安装了 windddk
#include <windows.h>
#define checktime( fun ) { \
int diffsec, diffusec; \
SYSTEMTIME tstart, tend; \
FILETIME tfilestart, tfileend;\
GetSystemTime( &tstart ); \
fun; \
GetSystemTime( &tend ); \
SystemTimeToFileTime( &tstart , &tfilestart); \
diffusec = tend.wMilliseconds - tstart.wMilliseconds ; \
diffsec = tend.wSecond - tstart.wSecond ; \
if ( diffusec < 0 ) \
{ \
diffsec -=1; \
diffusec +=1000; \
} \
char buf[32] = { 0 }; \
strncpy( buf, sizeof( buf ) - 1, #fun ); \
printf( "%s: %d.%03ds\n", buf, diffsec, diffusec );\
};strncpy( buf, sizeof( buf ) - 1, #fun ); \
printf( "%s: %d.%03ds\n", buf, diffsec, diffusec );\
int myfun( int i )
{
printf( "i=%d\n", i );
Sleep(2123);
return 0;
}
int main(int argc, char* argv[])
{
checktime( myfun(2) );
system( "pause" );
return 0;
}