zoukankan      html  css  js  c++  java
  • 测试函数的执行时间

    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;        \
                     }                    \
                    char buf[32] = { 0 };    \
                    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;        \
                     }                    \
                     char buf[32] = { 0 };    \
                    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;
    }
  • 相关阅读:
    zTree的学习
    spring第9天(事务)
    spring学习第8天(advisor)
    spring学习第7天(PCD以及切点表达式)
    学习spring第6天(aop获取目标方法参数)
    学习spring第五天 mybatis+spring的整合(maven多模块数据查询使用了分页和连接池),以及aop
    学习spring的第4天
    Arcgis for Js之GeometryService实现测量距离和面积
    Arcgis for Js实现graphiclayer的空间查询(续)
    Arcgis for Js实现graphiclayer的空间查询
  • 原文地址:https://www.cnblogs.com/diylab/p/1372313.html
Copyright © 2011-2022 走看看