Sign in to follow this  
jchmack

a question about cout

Recommended Posts

jchmack    187
i have an app which i use the console for debugging but when when i distribute it i'm planning on taking off the console. If i have a statement like: cout << "some debug info" << info << endl; and i have the console turned off. Will it take up CPU Time? Do you think its worth it to remove these debug statements when i release it?

Share this post


Link to post
Share on other sites
ToohrVyk    1596
It will take CPU time. Whether or not it is relevant depends on where the code actually is (during the startup sequence? in a tight loop?)

A solution would be to use a differently-named object for logging, such as debug. Then, depending on whether you're in a debug mode or in a non-debug mode, either bind that object to std::cout (or, as a personal preference, std::clog), or create a dummy ignored class for that object, and define an output operator which does strictly nothing, and will therefore be completely eliminated by the compiler:


#ifdef NDEBUG
class ignored {};

ignored debug;

template<typename T>
ignored & operator << (ignored & i, const T & other)
{
return i;
}
#else
std::ostream & debug = std::clog;
#endif

// Later in source

debug << "Some debug info " << info << std::endl;



Of course, this has the advantage of not breaking existing code which outputs the results of mandatory function calls (the display is not performed anymore, but the function call still is performed).

Share this post


Link to post
Share on other sites
Kambiz    758
Use the #if directive :

#ifdef _DEBUG
cout << "some debug info" << info << endl;
#endif

The compiler will evaluate the code between #if and #endif only in debug mode.

Share this post


Link to post
Share on other sites
TheAdmiral    1122
Quote:
Original post by Kambiz
Use the #if directive :

#ifdef _DEBUG
cout << "some debug info" << info << endl;
#endif

The compiler will evaluate the code between #if and #endif only in debug mode.

Peppering your source with precompiler directives causes source-bloat and confusion. A better alternative would be to wrap the directive conditional inside an inline function.

Admiral

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this