Monday, November 8, 2010

An Easy to Use Profiling Class

My professors always want profiling data...

It is really quite a bother to profile my code accurately. The explosion of timers and temporary variable always makes my code degenerate into an ugly mess.

The other day I decided to bite the bullet and make my own profiling library, one that I would truly enjoy using. I'm proud to say I finished yesterday, and I've put the finishing touches on it today.

My library adds very little code to your project and provides very accurate results. Rather than tell you all about my library, let's look at some quick examples to demonstrate how simple it is.

My Foo Function
void foo() { for (int i = 0; i < 99999; ++i) ; int q = 1; q *= (int)((4.3 * 23.43 * 23.454) / 234.32); }
My Foo Function With Profiling
void foo() { PF(); // Alias for PROFILE_FUNCTION() for (int i = 0; i < 99999; ++i) ; int q = 1; q *= (int)((4.3 * 23.43 * 23.454) / 234.32); }

As you can see, adding profiling into your code is a simple matter. Only one line of code required. If you were to execute that function, your program would automatically output the results of the profiling to the text file profile.txt when your program terminates (this is accomplished by a callback function added to the list of functions maintained by atexit()). And if you'd like to stop any profiling data from being collected, you may simply define NO_PROFILE at the top of your main.cpp file.

That's not the only thing you can do though, perhaps you'd like to profile a block of code, and not necessarily an entire function.

My Foo Function
void foo() { for (int i = 0; i < 99999; ++i) ; int q = 1; q *= (int)((4.3 * 23.43 * 23.454) / 234.32); }
My Foo Function With a Little Bit of Profiling
void foo() { for (int i = 0; i < 99999; ++i) ; PROFILE_CODE() { int q = 1; q *= (int)((4.3 * 23.43 * 23.454) / 234.32); } }

It's all very simple. There is even support for partitioning, however, I find it difficult to explain exactly what that means so I suggest taking a look at the main.cpp.txt file that is included with the header files to see a comprehensive overview of the features.

Below is some example profiling output. This is the output of the main.cpp.txt example file that is included with the header files.

~Profiling data output Mon Nov  8 15:01:45 2010

Profile for "Strange Char Operations"
 Ran in 0.000000 seconds. Tested 19900 times.
Profile for "The doMoreStuff() function"
 Ran in 0.006900 seconds. Tested 100 times.
Profile for "function doStuff()"
 Ran in 0.000063 seconds. Tested 10100 times.
Profile for "function linear()"
 Partition 0   ran in 0.000000 seconds. Tested 10  time(s).
 Partition 1   ran in 0.006000 seconds. Tested 10  time(s).
 Partition 2   ran in 0.005000 seconds. Tested 10  time(s).
 Partition 3   ran in 0.009000 seconds. Tested 10  time(s).
 Partition 4   ran in 0.013000 seconds. Tested 10  time(s).
 Partition 5   ran in 0.013000 seconds. Tested 10  time(s).
 Partition 6   ran in 0.017000 seconds. Tested 10  time(s).
 Partition 7   ran in 0.018000 seconds. Tested 10  time(s).
 Partition 8   ran in 0.022000 seconds. Tested 10  time(s).
 Partition 9   ran in 0.025000 seconds. Tested 10  time(s).
 Average time for each partition is 0.012800 seconds.
 Total time for all partitions is 0.128000 seconds.
Profile for "main.cpp @ 35"
 Ran in 0.000064 seconds. Tested 10000 times.

You can find the code here (updated 11/9/2010), simply extract the folder into your source tree and include profile/profile.h in your main.cpp file.

I will be constantly updating the library as I find problems and/or add new features. I encourage you to check back whenever you'd like to use it to see if there is an update out. Also, if you'd like, you may leave me your email and I will alert you to new updates.

As always, please email me or post here if you find any bugs in the code so I may fix them.

2 comments:

  1. Thanks, I do like this profiling code. I was looking for a better method than my homebrew class!

    ReplyDelete
  2. Glad you like it! I'll be uploading a new version very soon (in the next few days). There were some major bugs in the library that wouldn't even let it compile on non Unix systems.
    I've also added a couple more features which should prove to be very useful.

    ReplyDelete