# How to code profile.

This topic is 4215 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've searched all over the forums for how to profile code, but I always end up with just the suggestions TO profile, nothing else. Is this a functionality of some IDE, or a utility kinda like the gnudebugger? How do you profile your code?

##### Share on other sites
A good free profiler is AMD Codeanalyst. Although it's from AMD, it also works on Intel processors.

##### Share on other sites
Depends really on 3 things:
1. How much money you want to spend
2. What platform you are on
3. What language you are developing in.

The poorman's profiler is to just do:
int start = GetTickCount();.. run codeint end = GetTickCount();int totalTime = end - start

This works, but is a pain to use. So you can take the time to write your own full featured profiler like this.

Compuware gives out a toned down freeware (or is it just trial now?) version of its fantastic code profiler. You can find it here.

If you don't have the time or skills for that, then shelling out some cash might be best. There are a few alternatives so look at Wikipedia which has a huge link selection, so peruse them.

If you are on Linux there is one built into the system (I can't remember the name though..).

Hope this helps somewhat.

##### Share on other sites
Of course if you use Dev-C++ you can use the simple built in profiler.

##### Share on other sites
If you use Linux/gcc:

- compile using -pg.
- run the executable
- do $gprof executablenamehere > resultsfilehere Maybe there is a windowsport of gprof, I don't really know. #### Share this post ##### Link to post ##### Share on other sites Quote:  Original post by hydrooIf you use Linux/gcc:- compile using -pg.- run the executable- do$ gprof executablenamehere > resultsfilehereMaybe there is a windowsport of gprof, I don't really know.

Ah thats right. =) Can't believe I'd forgotten that, considering I was using that in my last sem's class!

##### Share on other sites
Don't use GetTickCount. It has a resolution in the millisecond range. Unless your code is really slow, or you're timing entire frames, you need a higher resolution such as from QueryPerformanceCounter.

##### Share on other sites
Quote:
 Original post by DeyjaDon't use GetTickCount. It has a resolution in the millisecond range. Unless your code is really slow, or you're timing entire frames, you need a higher resolution such as from QueryPerformanceCounter.

It's more than that though, arbitrary profiling like that tends to produce results that are baised either for or against the code in question. Your code needs to be profiled in the usage domain that is applicable to the situation. The standard method of
Get start time    Do something that is expensiveGet end time

Has several problems in that it doesn't actually mimic a real world usage pattern. For instance, profiling dot product code where you perform a dot product many thousands of times linearly likely isn't a very good benchmark, because you will usually be performing something with the results of the dot product that could introduce more overhead, or that could cause events like cache flushes that could end up being more expensive than your dot product was.

##### Share on other sites
Quote:
 Original post by DeyjaDon't use GetTickCount. It has a resolution in the millisecond range. Unless your code is really slow, or you're timing entire frames, you need a higher resolution such as from QueryPerformanceCounter.

A lot of people have noticed QPC is buggy on modern processors. timeGetTime with 1ms granularity is probably a safer option. Anyway, continue.

##### Share on other sites
Quote:
Original post by skittleo
Quote:
 Original post by DeyjaDon't use GetTickCount. It has a resolution in the millisecond range. Unless your code is really slow, or you're timing entire frames, you need a higher resolution such as from QueryPerformanceCounter.

A lot of people have noticed QPC is buggy on modern processors. timeGetTime with 1ms granularity is probably a safer option. Anyway, continue.

Actually, its not buggy, its just that if you do not set the processor affinity, your QPC results will be dependent upon the core that your thread executes on. Windows will attempt to keep your threads localized to a single core, but...not always. As such, due to different execution speeds of the various cores (since clock rates will vary per core), you may end up with different values being returned by QPC. To fix this is simply a matter of setting your processor affinity.

1. 1
2. 2
3. 3
4. 4
Rutin
12
5. 5

• 12
• 16
• 10
• 14
• 10
• ### Forum Statistics

• Total Topics
632659
• Total Posts
3007692
• ### Who's Online (See full list)

There are no registered users currently online

×