| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5031 人关注过本帖
标题:统计函数执行时间
只看楼主 加入收藏
s_k_y
Rank: 1
等 级:新手上路
帖 子:113
专家分:0
注 册:2008-2-2
结帖率:44.44%
收藏
已结贴  问题点数:0 回复次数:16 
统计函数执行时间
怎么在linux中的统计一个函数的执行时间?(精确到毫秒)
   
  调用系统函数夹在要统计函数的两端,之后在做差,这样统计时间好像不准啊?
搜索更多相关主题的帖子: 统计函数 时间 
2009-07-20 10:47
flyue
Rank: 10Rank: 10Rank: 10
来 自:江南西道
等 级:贵宾
威 望:19
帖 子:3465
专家分:1563
注 册:2006-6-20
收藏
得分:1 
time.h里有时间函数

一般统计一个函数执行时间就是把时间函数放在要检测的函数前后  然后求差

天之道,损有余而补不足.人之道则不然,损不足以奉有余.孰能有余以奉天下,唯有道者.
2009-07-20 10:57
s_k_y
Rank: 1
等 级:新手上路
帖 子:113
专家分:0
注 册:2008-2-2
收藏
得分:0 
回复 flyue
你的意思是这样吗?
  
  time_t s_time e_time;
  
  s_time=time(NULL);
  fun();//要统计的函数
  e_time=time(NULL);

  printf("time=%f\n",difftime(e_time,s_time));

  用很多时间函数,但我觉的这样的方法不对,我不知还有别的没有,比如定时器
  几次时间统计出来相差比较大。。。
2009-07-20 11:12
mfh
Rank: 6Rank: 6
等 级:侠之大者
帖 子:179
专家分:411
注 册:2009-5-31
收藏
得分:1 
2009-07-20 17:20
uubird
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:74
专家分:133
注 册:2009-7-2
收藏
得分:1 
说实话,我也很想知道~~因为我对时间的库函数不懂!!
高手们。。说说吧
2009-07-20 18:47
StarWing83
Rank: 8Rank: 8
来 自:仙女座大星云
等 级:贵宾
威 望:19
帖 子:3951
专家分:748
注 册:2007-11-16
收藏
得分:0 
写好程序,用gcc编译,带上-pg参数,然后运行以后分析gmon.out文件。

starwing@starwing-desktop:~/Work/prj$ cat test.c
#include <stdio.h>
#include <stdlib.h>

int func(int i)
{
    return i <= 2 ? 1 : func(i - 1) + func(i - 2);
}

int main(void)
{

    printf("zzz: %d\n", func(20));
    return 0;
}
/* cc: cmd='' */
starwing@starwing-desktop:~/Work/prj$ gcc -o test -pg test.c
starwing@starwing-desktop:~/Work/prj$ ./test
zzz: 6765
starwing@starwing-desktop:~/Work/prj$ gprof ./test ./gmon.out
Flat profile:

Each sample counts as 0.01 seconds.
 no time accumulated

  %   cumulative   self              self     total           
 time   seconds   seconds    calls  Ts/call  Ts/call  name   
  0.00      0.00     0.00        1     0.00     0.00  func

 %         the percentage of the total running time of the
time       program used by this function.

cumulative a running sum of the number of seconds accounted
 seconds   for by this function and those listed above it.

 self      the number of seconds accounted for by this
seconds    function alone.  This is the major sort for this
           listing.

calls      the number of times this function was invoked, if
           this function is profiled, else blank.
 
 self      the average number of milliseconds spent in this
ms/call    function per call, if this function is profiled,
       else blank.

 total     the average number of milliseconds spent in this
ms/call    function and its descendents per call, if this
       function is profiled, else blank.

name       the name of the function.  This is the minor sort
           for this listing. The index shows the location of
       the function in the gprof listing. If the index is
       in parenthesis it shows where it would appear in
       the gprof listing if it were to be printed.


             Call graph (explanation follows)


granularity: each sample hit covers 4 byte(s) no time propagated

index % time    self  children    called     name
                               13528             func [1]
                0.00    0.00       1/1           main [7]
[1]      0.0    0.00    0.00       1+13528   func [1]
                               13528             func [1]
-----------------------------------------------

 This table describes the call tree of the program, and was sorted by
 the total amount of time spent in each function and its children.

 Each entry in this table consists of several lines.  The line with the
 index number at the left hand margin lists the current function.
 The lines above it list the functions that called this function,
 and the lines below it list the functions this one called.
 This line lists:
     index    A unique number given to each element of the table.
        Index numbers are sorted numerically.
        The index number is printed next to every function name so
        it is easier to look up where the function in the table.

     % time    This is the percentage of the `total' time that was spent
        in this function and its children.  Note that due to
        different viewpoints, functions excluded by options, etc,
        these numbers will NOT add up to 100%.

     self    This is the total amount of time spent in this function.

     children    This is the total amount of time propagated into this
        function by its children.

     called    This is the number of times the function was called.
        If the function called itself recursively, the number
        only includes non-recursive calls, and is followed by
        a `+' and the number of recursive calls.

     name    The name of the current function.  The index number is
        printed after it.  If the function is a member of a
        cycle, the cycle number is printed between the
        function's name and the index number.


 For the function's parents, the fields have the following meanings:

     self    This is the amount of time that was propagated directly
        from the function into this parent.

     children    This is the amount of time that was propagated from
        the function's children into this parent.

     called    This is the number of times this parent called the
        function `/' the total number of times the function
        was called.  Recursive calls to the function are not
        included in the number after the `/'.

     name    This is the name of the parent.  The parent's index
        number is printed after it.  If the parent is a
        member of a cycle, the cycle number is printed between
        the name and the index number.

 If the parents of the function cannot be determined, the word
 `<spontaneous>' is printed in the `name' field, and all the other
 fields are blank.

 For the function's children, the fields have the following meanings:

     self    This is the amount of time that was propagated directly
        from the child into the function.

     children    This is the amount of time that was propagated from the
        child's children to the function.

     called    This is the number of times the function called
        this child `/' the total number of times the child
        was called.  Recursive calls by the child are not
        listed in the number after the `/'.

     name    This is the name of the child.  The child's index
        number is printed after it.  If the child is a
        member of a cycle, the cycle number is printed
        between the name and the index number.

 If there are any cycles (circles) in the call graph, there is an
 entry for the cycle-as-a-whole.  This entry shows who called the
 cycle (as parents) and the members of the cycle (as children.)
 The `+' recursive calls entry shows the number of function calls that
 were internal to the cycle, and the calls entry for each member shows,
 for that member, how many times it was called from other members of
 the cycle.



Index by function name

   [1] func
starwing@starwing-desktop:~/Work/prj$

专心编程………
飞燕算法初级群:3996098
我的Blog
2009-07-20 21:17
vfdff
Rank: 6Rank: 6
等 级:侠之大者
威 望:8
帖 子:2172
专家分:425
注 册:2005-7-15
收藏
得分:1 
为什么我得到的是乱字符呢
难道不能使用 cat gmon.out 看吗 ?

乱字符.JPG (57 KB)
图片附件: 游客没有浏览图片的权限,请 登录注册

~~~~~~~~~~~~~~~好好学习~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2009-07-20 22:13
prankmoon
Rank: 8Rank: 8
等 级:蝙蝠侠
帖 子:161
专家分:921
注 册:2009-7-21
收藏
得分:1 
下面是我写的一段代码,希望能对楼主有所帮助:

/**
 *      file_name:      time_lost.c
 *    description:
 *
 *        version:      1.0
 *        created:      3:32 2009-7-21
 *       revision:      none
 *       compiler:      gcc 4.3.3
 *
 *         author:      prankmoon@
 *        company:
 */

#include <stdio.h>
#include <stdlib.h>

/* just for test */
int foo(int n)
{
    int i;
    int sum = 0;
    for (i=0; i<n; i++)
    {
        sum += n;
    }

    return sum;
}

/* test function */
void time_lost()
{
    unsigned int start_high, start_low;
    unsigned int end_high, end_low;
    long long time_lost, start, end;

    /* here is the core :D */
    asm("rdtsc \n\t");
    asm("movl %%eax, %0\n\t":"=g"(start_low));
    asm("movl %%edx, %0\n\t":"=g"(start_high));
    printf("start_high:\t%08X start_low:\t%08X\n", start_high, start_low);
    start = start_high;
    start = (start << 32) | start_low;

    /* invoke the target function */
    foo(100);

    asm("rdtsc \n\t");
    asm("movl %%eax, %0\n\t":"=g"(end_low));
    asm("movl %%edx, %0\n\t":"=g"(end_high));
    printf("end_high:\t%08X end_low:\t%08X\n", end_high, end_low);
    end = end_high;
    end = (end << 32) | end_low;
   
    /* we did it */
    printf("lost time is:\t%llX\n", end - start);
   
    return;
}

int main(int argc, char *argv[])
{
    time_lost();

    return 0;
}

[[it] 本帖最后由 prankmoon 于 2009-7-21 03:58 编辑 [/it]]
2009-07-21 03:54
StarWing83
Rank: 8Rank: 8
来 自:仙女座大星云
等 级:贵宾
威 望:19
帖 子:3951
专家分:748
注 册:2007-11-16
收藏
得分:1 
以下是引用vfdff在2009-7-20 22:13的发言:

为什么我得到的是乱字符呢
难道不能使用 cat gmon.out 看吗 ?


gmon.out里面是二进制的文件,必须使用gprof程序查看。

专心编程………
飞燕算法初级群:3996098
我的Blog
2009-07-21 05:51
wxjeacen
Rank: 7Rank: 7Rank: 7
等 级:禁止访问
帖 子:1291
专家分:628
注 册:2009-3-22
收藏
得分:1 
直接time ./test不就ok.搞那么复杂干嘛。

生命不熄,战斗不止.
2009-07-21 06:58
快速回复:统计函数执行时间
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.042108 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved