hi
I find myself using stringstream all over my code just to build debug messages that I may or may not write to cout later. that is very comfortable while developing but is generally useless for productive code.
so I would like to have some construct that the compiler will remove in the case of NDEBUG. but I dont want to write #ifndef NDEBUG everywhere.
I have tried with this, but seemingly the compiler still cant completely remove NullStream because if I raise the loopcount to something like 10e7 it takes 3 seconds (with MSVS 2005 /O2, even longer with cygwin/gcc344 or ubuntu7.1/gcc413 -O3)
[code=c]
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class NullStream
{
public:
inline const NullStream& operator<< ( const string & s ) const
{
return *this;
}
inline const NullStream& operator<< ( int& i ) const
{
return *this;
}
inline const NullStream& operator<< ( unsigned int& i ) const
{
return *this;
}
};
inline ostream& operator<<( ostream& o, const NullStream& n)
{
return o;
}
inline ostream& operator<<( ostream& o, const stringstream& s )
{
o << s.str();
return o;
}
#ifdef NDEBUG
typedef NullStream LogStream;
#else
typedef ostringstream LogStream;
#endif
void main_logstream( )
{
cout << __FUNCTION__ << "\n";
LogStream buf;
double clock_begin = clock();
for ( unsigned int i = 0 ; i < 10e7 ; i++ )
{
buf << i << " ";
}
cout << "buffer: " << buf << "\n";
cout << "time: " << ( ( clock() - clock_begin ) / CLOCKS_PER_SEC ) << " s\n";
}
[/code]
I had expected that the compiler should be able to see that the body of that loop only consistst of a couple of calls to functions with empty bodies and thus the whole loop can be removed.
Anybody any suggestions?
I find myself using stringstream all over my code just to build debug messages that I may or may not write to cout later. that is very comfortable while developing but is generally useless for productive code.
so I would like to have some construct that the compiler will remove in the case of NDEBUG. but I dont want to write #ifndef NDEBUG everywhere.
I have tried with this, but seemingly the compiler still cant completely remove NullStream because if I raise the loopcount to something like 10e7 it takes 3 seconds (with MSVS 2005 /O2, even longer with cygwin/gcc344 or ubuntu7.1/gcc413 -O3)
[code=c]
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class NullStream
{
public:
inline const NullStream& operator<< ( const string & s ) const
{
return *this;
}
inline const NullStream& operator<< ( int& i ) const
{
return *this;
}
inline const NullStream& operator<< ( unsigned int& i ) const
{
return *this;
}
};
inline ostream& operator<<( ostream& o, const NullStream& n)
{
return o;
}
inline ostream& operator<<( ostream& o, const stringstream& s )
{
o << s.str();
return o;
}
#ifdef NDEBUG
typedef NullStream LogStream;
#else
typedef ostringstream LogStream;
#endif
void main_logstream( )
{
cout << __FUNCTION__ << "\n";
LogStream buf;
double clock_begin = clock();
for ( unsigned int i = 0 ; i < 10e7 ; i++ )
{
buf << i << " ";
}
cout << "buffer: " << buf << "\n";
cout << "time: " << ( ( clock() - clock_begin ) / CLOCKS_PER_SEC ) << " s\n";
}
[/code]
I had expected that the compiler should be able to see that the body of that loop only consistst of a couple of calls to functions with empty bodies and thus the whole loop can be removed.
Anybody any suggestions?
Comment