measure time in a function in C

后端 未结 5 2092
南方客
南方客 2021-02-09 17:14

I\'m debugging an C application and I\'d like to know how much time it spends in a particular function.

I could change the source code and add some more code to do the m

5条回答
  •  孤独总比滥情好
    2021-02-09 17:37

    I have a helper function in my ~/.gdbinit:

    define timeme
    set $last=clock()
    n
    set $timing=clock() - $last
    if $timing>$arg0
    printf "***long***\n"
    end
    printf "%d cycles, %f seconds\n", $timing, (float)$timing / 1000000
    end
    

    You may need to adjust the 1000000 depending on what your implementation of CLOCKS_PER_SEC is on your platform.

    Usage is trivial; run the helper which will execute the next step and give timing information:

    Breakpoint 2, install_new_payload_from_meta (snmp_meta=0x7eee81c0, pkt=0x0, entry=0x7d4f4e58) at /home/sgillibr/savvi-dc-snmp/recipies.c:187
    (gdb) timeme 100000
    ***long***
    580000 cycles, 0.580000 seconds
    (gdb)
    

    Obviously resolution may not be enough for some needs, although it does prove very useful.

提交回复
热议问题