Question: What method do you use when you want to get performance data about specific code paths?

Answer: 
I do my profiles by creating two classes: `cProfile` and `cProfileManager`.

`cProfileManager` will hold all the data that resulted from `cProfile`.

`cProfile` with have the following requirements:

* `cProfile` has a constructor which initializes the current time.
* `cProfile` has a deconstructor which sends the total time the class was alive to `cProfileManager`

To use these profile classes, I first make an instance of `cProfileManager`. Then, I put the code block, which I want to profile, inside curly braces. Inside the curly braces, I create a `cProfile` instance. When the code block ends, `cProfile` will send the time it took for the block of code to finish to `cProfileManager`.

**Example Code**
Here's an example of the code (simplified):

```
class cProfile
{
    cProfile()
    {
        TimeStart = GetTime();
    };

    ~cProfile()
    {
        ProfileManager->AddProfile (GetTime() - TimeStart);
    }

    float TimeStart;
}

```

To use `cProfile`, I would do something like this:

```
int main()
{
    printf("Start test");
    {
        cProfile Profile;
        Calculate();
    }
    ProfileManager->OutputData();
}

```

or this:

```
void foobar()
{
    cProfile ProfileFoobar;

    foo();
    {
        cProfile ProfileBarCheck;
        while (bar())
        {
            cProfile ProfileSpam;
            spam();
        }
    }
}

```

**Technical Note**

This code is actually an abuse of the way scoping, constructors and deconstructors work in [C++](http://en.wikipedia.org/wiki/C_%28programming_language%29). `cProfile` exists only inside the block scope (the code block we want to test). Once the program leaves the block scope, `cProfile` records the result.

**Additional Enhancements**

* You can add a string parameter to the constructor so you can do something like this:
cProfile Profile("Profile for complicated calculation");
* You can use a macro to make the code look cleaner (be careful not to abuse this. Unlike our other abuses on the language, macros can be dangerous when used).

Example:

#define START\_PROFILE cProfile Profile(); {
#define END\_PROFILE }
* `cProfileManager` can check how many times a block of code is called. But you would need an identifier for the block of code. The first enhancement can help identify the block. This can be useful in cases where the code you want to profile is inside a loop (like the second example aboe). You can also add the average, fastest and longest execution time the code block took.
* Don't forget to add a check to skip profiling if you are in debug mode.
The article *Code profiler and optimizations* has lots of information about C++ code profiling and also has a free download link to a program/class that will show you a graphic presentation for different code paths/methods.