March 11, 2005
The power of macros
Posted by Matt in
Tech Talk
Wow, another blog from me within two days of each other. I have been working at increasing the efficiency of our software and in the process and as a little reward to myself, I have taken today off and possibly Monday. It is a welcomed break from the pre-show hustle and bustle and the post show modifications. Lately we have been focusing on increasing the efficiency of our software and I needed an easy way to time how long it took for a function to execute. A timer is the best way to do it but in C++, the best I could find were the CTime and CTimeSpan objects. We have used those in the past but they are only accurate to seconds. I needed something a bit better that was easy, or easier to use. Ya see, it all relates back to my previous post about saving key strokes. I could write up a class to handle all this for me or just a global function but in the end, everywhere I used the timer, the number of keystrokes in comparison to the macro would be exponential. That’s the purpose of a macro anyway, to save time, or in this case, to tell it.
In C++, there are a lot of practices that are frowned upon but they exist. And they exist for a reason. The problem is, most people abuse them because they are lazy. In my case, yes there is a bit of laziness, but the macros I have developed are only used in debug versions of the software. Once the software is ready for release, the macros redefine themselves to nothing. In this situation, I see no problems using them. Below is the macro
KEY:
Comment Color
String Color
Object Type Color
Code Color
If you can't guess it, i am a big fan of using color in code to provide easier readablity. Most development environments provide customizable options to allow for this. I higly suggest looking into it as it makes reading code 10 times easier.
#ifdef _DEBUG
#define BEGIN_TIMER(x,y,z) \
DWORD x ## y = ::GetTickCount(); \
CString x ## y ## z(#x); \
TRACE(_T("Timer %s Started...\n"),x ## y ## z)
#define START_TIMER(x) BEGIN_TIMER(x,Begin,End)
#define FINISH_TIMER(x,y,z) \
DWORD x ## z = ::GetTickCount(); \
TRACE(_T("Timer %s finished in %f seconds\n"), x ## y ## z, ((x ## z) - (x ## y))/1000.0)
#define END_TIMER(x) FINISH_TIMER(x,Begin,End)
#else
#define START_TIMER(x)
#define END_TIMER(x)
#endif
Here is an example of its use
START_TIMER(timeUntilNextBlog);
... // do normal everyday stuff
END_TIMER(timeUntilNextBlog);
This basically expands to
DWORD timeUntilNextBlogBegin = ::GetTickCount();
CString timeUntilNextBlogBeginEnd(“timeUntilNextBlog”);
TRACE(_T(“Timer %s Started…\n”), timeUntilNextBlogBeginEnd);
… // do normal everday stuff
DWORD timeUntilNextBlogEnd = ::GetTickCount();
TRACE(_T(“Timer %s finished in %f seconds\n”), timeUntilNextBlogBeginEnd, ((timeUntilNextBlogEnd) – (timeUntilNextBlogBegin))/1000.0);
And the output would be
Timer timeUntilNextBlog started…
Timer timeUntilNextBlog finished in 172000.000000 seconds
Hope some of you can use this. It was a big help to us for increasing the efficiency of our software. For those of you not using Microsoft's DevStudio or .Net compiler, the TRACE portion may need to be removed. It is a macro defined by Microsoft make outputting to the debug window easier. Go figure :-P
Permalink
|
TrackBack (0)
Comments
I'm having trouble setting up the colours in Visual Notepad. Can I get some pointers?