16

I've used a few profilers in the past and never found them particularly easy. Maybe I picked bad ones, maybe I didn't really know what I was expecting! But I'd like to know if there are any 'standard' profilers which simply drop in and work? I don't believe I need massively fine-detailed reports, just to pick up major black-spots. Ease of use is more important to me at this point.

It's VC++ 2008 we're using (I run standard edition personally). I don't suppose there are any tools in the IDE for this, I can't see any from looking at the main menus?

Mr. Boy
  • 60,845
  • 93
  • 320
  • 589

6 Answers6

31

I suggest a very simple method (which I learned from reading Mike Dunlavey's posts on SO):

Just pause the program.

Do it several times to get a reasonable sample. If a particular function is taking half of your program's execution time, the odds are that you will catch it in the act very quickly.

If you improve that function's performance by 50%, then you've just improved overall execution time by 25%. And if you discover that it's not even needed at all (I have found several such cases in the short time I've been using this method), you've just cut the execution time in half.

I must confess that at first I was quite skeptical of the efficacy of this approach, but after trying it for a couple of weeks, I'm hooked.

Community
  • 1
  • 1
Michael Myers
  • 188,989
  • 46
  • 291
  • 292
  • 11
    ++ Yeah, I'm afraid I've made a royal pest of myself advertising that technique, but darn it, it works. (The important thing is to look at the call stack. Sometimes people say "It's in some system routine - what good is that?" when the problem is obvious a few levels up.) – Mike Dunlavey Apr 12 '10 at 20:01
  • 1
    I was skeptical. But, I can confirm that this actually works. Specially, if your program performs noticeably slow. Best of all, there's no need of code hooks. – dev_nut Mar 23 '16 at 21:53
13

VS built in:

If you have team edition you can use the Visual Studio profiler.


Other options:

Otherwise check this thread.


Creating your own easily:

I personally use an internally built one based on the Win32 API QueryPerformanceCounter. You can make something nice and easy to use within a hundred lines of code or less.

The process is simple: create a macro at the top of each function that you want to profile called PROFILE_FUNC() and that will add to internally managed stats. Then have another macro called PROFILE_DUMP() which will dump the outputs to a text document.

PROFILE_FUNC() creates an object that will use RAII to log the amount of time until the object is destroyed. Both the constructor of this RAII object and the destructor will call QueryPerformanceCounter. You could also leave these lines in your code and control the behavior via a #define PROFILING_ON

Community
  • 1
  • 1
Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636
  • Trying Sleepy, not entirely sure about it yet. – Mr. Boy Apr 12 '10 at 19:55
  • 1
    I've hated how Microsoft took a great tool (Visual Studio Professional) and priced great features like the profiler completely out of my reach. So instead of getting a guaranteed $600 from me, I'm just going to use mingw and use trial and error. – Chris K Apr 12 '10 at 20:07
  • @Chris: I literally *cannot believe* the sense of entitlement you have. MS offers an outstanding optimising compiler and IDE for $0, and still people whine. You realise they need to keep *some* desirable features out of the freebie version so that people will buy Pro, right? – j_random_hacker Oct 14 '11 at 04:35
  • 5
    @j_random_hacker: I guess I'm used to product upgrades GIVING me new stuff, not taking stuff I used to have access to away from me. Visual Studio 6 Professional gave me a profiler - I'd have to buy Visual Studio Premium to get that feature today. $699 in 1998 vs. $4300 today. Entitlement my ass. That's gouging. – Chris K Nov 01 '11 at 23:33
  • 6
    @Chris: Sorry, poor reading comprehension on my part -- I thought you were complaining that the free Express Edition didn't give you a profiler, and that you would need to pay $600 to get it. Like you I'd also expect a profiler in an edition called "Professional". – j_random_hacker Nov 02 '11 at 03:59
  • 1
    @j_random_hacker: no worries - I certainly like Microsoft's Express Edition products (SQL, Visual Studio). – Chris K Nov 02 '11 at 19:41
3

I always used AMD CodeAnalyst, I find it quite easy to use and gives interesting results. I always used the time based profile, in which I found that it cooperates well with my apps' debug information, letting me find where the time is spent at procedure, C++ instruction and single assembly instruction level.

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
2

I used lt prof in the past for a quick run down of my C++ app. It works pretty easy and runs with a compiled program, does not need and source code hooks or tweaks. There is a trial version available I believe.

Charles
  • 3,734
  • 3
  • 31
  • 49
  • ++ I just followed that link, and it looks promising. It clearly works by stack sampling and gives line-level resolution. I can't tell if it allows samples during I/O or other blocking, which would allow detecting needless I/O. – Mike Dunlavey Apr 12 '10 at 21:57
  • 1
    I just tried out LTProf. What I like is that it gives line-level percent, and it does sample during I/O (Yay!!). Then I was sad to see that there was no way to manually turn the sampling on/off, like with a hot key. Thus, if a program is interactive, there is no way to say "sample NOW", not when it's waiting for user input. – Mike Dunlavey Jun 05 '10 at 23:51
2

A very simple (and free) way to profile is to install the Windows debuggers (cdb/windbg), set a bp on the place of interest, and issue the wt command ("Trace and Watch Data"). Check out MSDN for more info.

nithins
  • 3,172
  • 19
  • 21
  • ++ I used windbg ages ago, and relied on the Ctrl-C method. Then if I wanted it to run slowly I would use a Watch Data, as you recommend. – Mike Dunlavey Apr 12 '10 at 22:01
1

Another super simple and useful profiling workflow that works on any programming languages is to comment out blocks of codes. After commenting out all of them, uncomment some and run your program to see the performance. If your program starts to run very slow when some code has been uncommented, then you'll probably want to check the performance there.

MohaElder
  • 41
  • 4
  • it seems there will be lots of manual work with this way – leiyc May 31 '22 at 06:33
  • That’s true for a project with a huge codebase. But it works super well for projects with small code bases like school assignments. Anyways, the question is asking for a “very easy” c++ profiler, I believe that my proposed profiling way is easy enough and also easy to understand because you usually see a lot of stl methods calls in a profiler and that could be confusing for beginners – MohaElder Jun 01 '22 at 16:39
  • This is a good skill to know. Not saying it’s a better idea but nonetheless it’s a good tool to add to your bag. Personally I feel more connected to the code base once I manually walk through it like this. My 2 cents – JSON Nov 05 '22 at 03:34