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;
    }
  • 相关阅读:
    Unity 3(一):简介与示例
    MongoDB以Windows Service运行
    动态SQL中变量赋值
    网站发布IIS后堆栈追踪无法获取出错的行号
    GridView Postback后出错Operation is not valid due to the current state of the object.
    Visual Studio 2010 SP1 在线安装后,找到缓存在本地的临时文件以便下次离线安装
    SQL Server 问题之 排序规则(collation)冲突
    IIS 问题集锦
    linux下安装mysql(ubuntu0.16.04.1)
    apt-get update 系列作用
  • 原文地址:https://www.cnblogs.com/diylab/p/1372313.html
Copyright © 2011-2022 走看看