measure time in a function in C

后端 未结 5 2091
南方客
南方客 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:46

    gprof is only reliable -- in my experience, only works at all -- if you statically link -pg compiled versions of every library, including the C library. You can try to do this using gcc's -profile option (which does what -pg does plus tries to sub in -pg libraries) but the problem is, GNU libc really does not like being statically linked, and your distro may not provide -pg compiled versions of every library you need.

    I suggest you try cachegrind, which is a valgrind mode of operation and only needs debug info for everything. That's much easier to get. The catch is, it has huge overhead costs; so huge that it might invalidate your testing. Expect at least a 2x slowdown.

    You can also try perf -- if you can get your hands on a copy. It's very clever, but it is of, by, and for kernel hackers who think people like building stuff from scratch. I have had very mixed luck with it. (Do read http://web.eecs.utk.edu/~vweaver1/projects/perf-events/ which is about the underlying API, not the utility, but might still save you from a great deal of wasted time.)

提交回复
热议问题