473,378 Members | 1,495 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Performance question/debugging

I am putting debugging messages into my program by putting blocks that
look like this:
#ifdef DEBUG
errlog << "Here is some information";
#endif

All these #ifdef blocks make the code bulky and harder to read, so I'd
like to do something where I put:
errMsg("Here is some information");

and then in a library:
void errMsg(string error)
{
#ifdef DEBUG
errlog << error;
#endif
}

My question is how this would affect the performance of the program.
Are compiler optimizers (specifically the GNU compiler) smart enough to
not spend CPU cycles calling this function and passing the argument
when DEBUG isn't defined, since the function does absolutely nothing?
Also, is there an elegant way to get rid of the "unused parameter"
warning.

I'd be interested to hear how others deal with putting debugging info
like this into their programs.

Thanks,
Colin

Jul 16 '06 #1
8 2189
ra*****@gmail.com wrote:
I am putting debugging messages into my program by putting blocks that
look like this:
#ifdef DEBUG
errlog << "Here is some information";
#endif

All these #ifdef blocks make the code bulky and harder to read, so I'd
like to do something where I put:
errMsg("Here is some information");

and then in a library:
void errMsg(string error)
Better to use const std::string&.
{
#ifdef DEBUG
errlog << error;
#endif
}

My question is how this would affect the performance of the program.
Are compiler optimizers (specifically the GNU compiler) smart enough to
not spend CPU cycles calling this function and passing the argument
when DEBUG isn't defined, since the function does absolutely nothing?
Also, is there an elegant way to get rid of the "unused parameter"
warning.
Most, if not all current compilers will inline and thus remove an empty
function.
I'd be interested to hear how others deal with putting debugging info
like this into their programs.
Either the way you have done it, or by creating a specialised stream.

--
Ian Collins.
Jul 16 '06 #2
razael1 wrote:
>I am putting debugging messages into my program by putting blocks that
look like this:
#ifdef DEBUG
errlog << "Here is some information";
#endif

All these #ifdef blocks make the code bulky and harder to read, so I'd
like to do something where I put:
errMsg("Here is some information");
One way to do this is via macro abuse. There are other ways, with different
trade-offs. I would not use the following for the log of a server program.
That should be a complete subsystem.

Debugging traces must be absurdly easy to write, because you must belt them
out while in the throws of debugging, without their details distracting you.
So try this:

#ifndef DEBUG_
# ifdef _DEBUG // this helper makes irritations go away when we are not
debugging
# define DEBUG_(Q_) Q_
# else
# define DEBUG_(Q_)
# endif
#endif

That uses a platform that defines _DEBUG. You might also switch based on the
Standard !defined(NDEBUG).

#ifdef _DEBUG
# define SOURCE__ __FILE__ "(" << __LINE__ << "): "
# define SPEW(x) (std::cout << SOURCE__ "TRACE - " #x ": " << x <<
std::endl)
#endif

That outputs a line in the same format as a Visual Studio error message
(following the coding practice "assertion failures are syntax errors").

At debug time, you can write this:

SPEW(bigObject << " shouldn't be " << stringFoo);

The operator<< will put anything into the trace, not just strings. Anything
with an operator<<.

The SPEW() disappears when you compile for release. This is a feature. It's
to remind you to take the SPEW() out, and not rely on it for too long.

If you need to commit your code with a SPEW() in it, use
DEBUG_(SPEW(whatever)).
My question is how this would affect the performance of the program.
Your code, which I snipped, would be slow, not because the inside of the
function could go away, but because the calling site of the function could
assemble a string from parts, expensively.

In release mode, you could instead #define errMsg(x) as nothing.
Also, is there an elegant way to get rid of the "unused parameter"
warning.
error=error just inside the function. Or this:

void errMsg(string DEBUG_(error))

My evil ugly DEBUG_() macro allows you write code that is either slick, or
trick. Avoid the latter, because when debugging code differs too far from
production code, you could catch "debugitis", which is bugs caused by
debugging too much.

/The C++ Programming Language, 3rd Ed/ by Bjarne S. contains an alternative
that uses a template, and is closer to a log system than a debugging trace
system. Also study that. I didn't, so it might improve my macro abuse.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Jul 16 '06 #3
I'm now trying to do this using inline functions, but I've run into
some problems. Here is a simplified version of what I have:

debug.h:
#ifdef DEBUG_H
#define DEBUG_H

#include <fstream>

static std::ofstream errlog;

void startDebug( )
{
#ifdef DEBUG
errlog.open("error.log");
errlog << "Starting log" << endl;
#endif
}

void endDebug()
{
#ifdef DEBUG
errlog << "Program exited normally" << endl;
errlog.close();
#endif
}

void errMsg(std::string& message)
{
#ifdef DEBUG
errlog << message << endl;
#endif
}

#endif

main.cpp:
#include "debug.h"
#include "func.h"
void main( )
{
startDebug( );
func();
endDebug();
}

func.cpp:
#include "debug.h"

void func( )
{
errMsg("Error");
}
I hope I haven't simplified this so far that I've gotten rid of the
error. After running my program, the message log contains "Starting
log" and "Program exited normally", but does not contain any of what
should have been written by the errMsg() function.

Thanks,
Colin

Jul 16 '06 #4

raza...@gmail.com wrote:
I'm now trying to do this using inline functions, but I've run into
some problems. Here is a simplified version of what I have:

debug.h:
#ifdef DEBUG_H
#define DEBUG_H

#include <fstream>

static std::ofstream errlog;

void startDebug( )
{
#ifdef DEBUG
errlog.open("error.log");
errlog << "Starting log" << endl;
#endif
}

void endDebug()
{
#ifdef DEBUG
errlog << "Program exited normally" << endl;
errlog.close();
#endif
}

void errMsg(std::string& message)
{
#ifdef DEBUG
errlog << message << endl;
#endif
}

#endif

main.cpp:
#include "debug.h"
#include "func.h"
void main( )
{
startDebug( );
func();
endDebug();
}

func.cpp:
#include "debug.h"

void func( )
{
errMsg("Error");
}
I hope I haven't simplified this so far that I've gotten rid of the
error. After running my program, the message log contains "Starting
log" and "Program exited normally", but does not contain any of what
should have been written by the errMsg() function.

Thanks,
Colin
In my code above, I forgot to 'inline' all the functions in debug.h.

Jul 16 '06 #5
In my code above, I forgot to 'inline' all the functions in debug.h.

Jul 16 '06 #6
ra*****@gmail.com wrote:
raza...@gmail.com wrote:
>>I'm now trying to do this using inline functions, but I've run into
some problems. Here is a simplified version of what I have:
You should try and post something that compiles and exhibits the problem
you are trying to solve.
>>debug.h:
#ifdef DEBUG_H
#define DEBUG_H

#include <fstream>

static std::ofstream errlog;
This should be an extern and defined in a cc file.
>>void startDebug( )
{
#ifdef DEBUG
errlog.open("error.log");
errlog << "Starting log" << endl;
Missing std::
>>#endif
}

void endDebug()
{
#ifdef DEBUG
errlog << "Program exited normally" << endl;
Missing std::
>>errlog.close();
#endif
}

void errMsg(std::string& message)
Missing const
>>{
#ifdef DEBUG
errlog << message << endl;
#endif
}

#endif

main.cpp:
#include "debug.h"
#include "func.h"
void main( )
main() return int.
>>{
startDebug( );
func();
endDebug();
}

func.cpp:
#include "debug.h"

void func( )
{
errMsg("Error");
}
I hope I haven't simplified this so far that I've gotten rid of the
error. After running my program, the message log contains "Starting
log" and "Program exited normally", but does not contain any of what
should have been written by the errMsg() function.
You probably have, when fixed, the posted code would work correctly.
Did you compile both source files with DEBUG defined?

--
Ian Collins.
Jul 16 '06 #7
razael1 wrote:
>void main( )
'void main' will make you regress to the late 1990s. Use 'int main'.

Next, debug statements, like my SPEW() system, are just that - for
debugging. If you leave these statements in your code, but don't turn them
into a proper log system (with a different architecture), then you are using
Debugger Driven Programming.

And that, in turn, is a sign you should be using Test Driven Programming.
That way, all the stuff you expect these errMsg() calls to tell the
programmer, you could instead tell to a test case. And it would read the
results for you, automatically, and only report to _you_ if there's some
descrepancy.

Look up TDD, and you will see what I mean. You are putting your energies in
the wrong direction. TDD is widely reported to prevent excessive debugging.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Jul 17 '06 #8
My question is how this would affect the performance of the program.

If your compiler is half-decent, then any empty functions will be vapourised.

However, if you want to be doubly sure, then move the #ifdef outside of the
function:

#ifdef NDEBUG
#define errMsg(str) /* Nothing */
#else
#define errMsg(str) (errlog << (str))
#endif
--

Frederick Gotham
Jul 17 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: lkrubner | last post by:
I've got a PHP application that's 2 megs in size. Of that, my guess is 200k-400k is comments. Do they impose a performance hit? I've been postponing any kind of optimization, but at some point I'll...
6
by: Scott CM | last post by:
I have a multi-part question regarding trigger performance. First of all, is there performance gain from issuing the following within a trigger: SELECT PrimaryKeyColumn FROM INSERTED opposed...
2
by: johdi | last post by:
Hey there, This is no doubt demonstrating how cruddy my understanding is - what are the performance implications of having left the Debug=true value in your web.config file but not...
13
by: Kurt Schroeder | last post by:
does compiling the code behind source using studio give and performance boost? this is a rephrase of an a question i posted yesterday. To be honist VS.net is becomming a pain to work with for...
1
by: Wade | last post by:
Hi all, We have a debate going on here in the office. There's one group that would prefer to have our LIVE environment with debugging turned on, because it's convenient when there's an error,...
3
by: Henrik | last post by:
Hi all, I'm having problem with high processor load on the system where I'm running my application. The system is a P4 3.0 GHz with 1 GB RAM. I would like to log the thread activity of my...
6
by: Bob | last post by:
Hi, I have a fairly large but not massive project. Visual Studio 2005 is starting to act very strange when I debug the project. For instance, if i set a break point, I often can't expand an...
4
by: JavaPhreak | last post by:
So I work for a firm where Java performance is highly debated (e.g. threading, garbage collection, overuse of abstraction, interprocess latency, write once, debug everywhere, debuggers for real-time...
12
by: Ilyas | last post by:
Hi all I have an application which brings back 1000 records from a sql server database. Under the local asp.net development server provided with Visual Studio 2008, it takes about 1.8 seconds ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.