?
Solved

Why is 'afxDump << etc' slower than TRACE(); in Visual C++ .net? Is it just me?

Posted on 2003-03-22
2
Medium Priority
?
281 Views
Last Modified: 2008-02-01
I've knocked up a quick demo app and added the following button handler:

void CafxDumpTimeTestDlg::OnBnClickedBtnStart()
{
     // TODO: Add your control notification handler code here
     DWORD dwStart = 0L;
     DWORD dwEnd = 0L;
     DWORD dwLapse = 0L;
     DWORD dwLoop = 0L;

     dwStart = ::GetTickCount();

     for(dwLoop = 0 ; dwLoop < 20 ; dwLoop++){
          afxDump << "Loop " << dwLoop << "\n";
     }

     dwEnd = ::GetTickCount();
     dwLapse = dwEnd - dwStart;

     afxDump << "==== afxDump Lapsed " << dwLapse << "\n";

     dwStart = ::GetTickCount();

     for(dwLoop = 0 ; dwLoop < 20 ; dwLoop++){
          TRACE("Loop %i\n", dwLoop);
     }

     dwEnd = ::GetTickCount();
     dwLapse = dwEnd - dwStart;

     afxDump << "==== TRACE Lapsed " << dwLapse << "\n";
}

and I get the results in the debug output window:
Loop 0
Loop 1
Loop 2
Loop 3
Loop 4
Loop 5
Loop 6
Loop 7
Loop 8
Loop 9
Loop 10
Loop 11
Loop 12
Loop 13
Loop 14
Loop 15
Loop 16
Loop 17
Loop 18
Loop 19
==== afxDump Lapsed 3575
Loop 0
Loop 1
Loop 2
Loop 3
Loop 4
Loop 5
Loop 6
Loop 7
Loop 8
Loop 9
Loop 10
Loop 11
Loop 12
Loop 13
Loop 14
Loop 15
Loop 16
Loop 17
Loop 18
Loop 19
==== TRACE Lapsed 1202


which makes 'TRACE()' maybe three times quicker than afxDump !
0
Comment
Question by:pcooke
2 Comments
 
LVL 8

Expert Comment

by:fl0yd
ID: 8188245
Why would you care, which one is faster/slower? Those are debug-functions and won't be present in a release build anyway, so why worry?

.f
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 375 total points
ID: 8189453
I got the same results.
To see why this happens debug TRACE and afxDump lines.
TRACE is executed using single call to unsafe but fast function of class CTraceFileAndLineInfo:

void __cdecl operator()(const char *pszFmt, ...) const

afxDump line is series of calls to overloaded

CDumpContext& CDumpContext ::operator<<

operators.

The line:
afxDump << "Loop " << dwLoop << "\n";

actually calls 3 overloaded << operators with parameters:
LPCTSTR
DWORD_PTR
LPCTSTR

LPCTSTR operator contains also some loops. All this, together with type conversions and returning reference to CDumpContext object, gets the time.

Time difference is the difference between unsafe and fast C-style TRACE implememtation and safe and slow C++ style CDumpContext implementation.
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question