How do you profile your code?

后端 未结 9 1859
小鲜肉
小鲜肉 2020-12-23 18:14

I hope not everyone is using Rational Purify.

So what do you do when you want to measure:

  • time taken by a function
  • peak memory usage
相关标签:
9条回答
  • 2020-12-23 18:47

    I use devpartner with MSVC 6 and XP

    0 讨论(0)
  • 2020-12-23 18:49

    For complex applications I am a great fan of Intel's Vtune. It is a slightly different mindset to a traditional profiler that instruments the code. It works by sampling the processor to see where instruction pointer is 1,000 times a second. It has the huge advantage of not requiring any changes to your binaries, which as often as not would change the timing of what you are trying to measure.

    Unfortunately it is no good for .net or java since there isn't a way for the Vtune to map instruction pointer to symbol like there is with traditional code.

    It also allows you to measure all sorts of other processor/hardware centric metrics, like clocks per instruction, cache hits/misses, TLB hits/misses, etc which let you identify why certain sections of code may be taking longer to run than you would expect just by inspecting the code.

    0 讨论(0)
  • 2020-12-23 18:53

    nProf - Free, does that for .NET.

    Gets the job done, at least enough to see the 80/20. (20% of the code, taking 80% of the time)

    0 讨论(0)
  • 2020-12-23 18:55

    I've done this a lot. If you have an IDE, or an ICE, there is a technique that takes some manual effort, but works without fail.

    Warning: modern programmers hate this, and I'm going to get downvoted. They love their tools. But it really works, and you don't always have the nice tools.

    I assume in your case the code is something like DSP or video that runs on a timer and has to be fast. Suppose what you run on each timer tick is subroutine A. Write some test code to run subroutine A in a simple loop, say 1000 times, or long enough to make you wait at least several seconds.

    While it's running, randomly halt it with a pause key and sample the call stack (not just the program counter) and record it. (That's the manual part.) Do this some number of times, like 10. Once is not enough.

    Now look for commonalities between the stack samples. Look for any instruction or call instruction that appears on at least 2 samples. There will be many of these, but some of them will be in code that you could optimize.

    Do so, and you will get a nice speedup, guaranteed. The 1000 iterations will take less time.

    The reason you don't need a lot of samples is you're not looking for small things. Like if you see a particular call instruction on 5 out of 10 samples, it is responsible for roughly 50% of the total execution time. More samples would tell you more precisely what the percentage is, if you really want to know. If you're like me, all you want to know is where it is, so you can fix it, and move on to the next one.

    Do this until you can't find anything more to optimize, and you will be at or near your top speed.

    0 讨论(0)
  • 2020-12-23 19:00

    If you're doing an 'on the metal' embedded 'C' system (I'm not quite sure what 'mobile' implied in your posting), then you usually have some kind of timer ISR, in which it's fairly easy to sample the code address at which the interrupt occurred (by digging back in the stack or looking at link registers or whatever). Then it's trivial to build a histogram of addresses at some combination of granularity/range-of-interest.

    It's usually then not too hard to concoct some combination of code/script/Excel sheets which merges your histogram counts with addresses from your linker symbol/list file to give you profile information.

    If you're very RAM limited, it can be a bit of a pain to collect enough data for this to be both simple and useful, but you would need to tell us a more about your platform.

    0 讨论(0)
  • 2020-12-23 19:06

    How are any tools going to work if your platform is a proprietary OS? I think you're doing the best you can right now

    0 讨论(0)
提交回复
热议问题