473,769 Members | 5,374 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2215
ra*****@gmail.c om 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(wha tever)).
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("er ror.log");
errlog << "Starting log" << endl;
#endif
}

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

void errMsg(std::str ing& 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.c om 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("er ror.log");
errlog << "Starting log" << endl;
#endif
}

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

void errMsg(std::str ing& 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.c om wrote:
raza...@gmail.c om 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::str ing& 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
2755
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 have to do it. Is taking out the comments worth it? Of all the optimizations I can do, where should it rank?
6
6557
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 to: SELECT * FROM INSERTED
2
2416
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 building/deploying the PDB symbol file? Thanks for your help.
13
1250
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 asp.net. Vb.net and c.net hey, no problem, but asp.net it seems to create way too much over head and complication. thanks for listening and would appreciate an opinion. thanks kes
1
1264
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, and another (myself included) that would prefer not to have debugging turned on. So, I ask you all: is there really a significant overhead in having this turned on? And by turned on, I mean that all PDB files are copied out into the \bin...
3
1799
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 application using performance counters to try to find out which thread that is causing the high processor load. Any ideas how to do this? How can I identify what part of my code a specific thrad is executing?
6
6389
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 array to see the contents. Instead, it will say "function evaluation timed out". Also, it takes a very long time to start and stop debugging, and sometimes it won't restart after I pause execution. I don't seem to have nearly as many problems in...
4
1814
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 apps, etc). The C and C++ applications developed in-house are used for our real-time systems (high speed prop trading systems) based on high volume/low latency mulit-cast messaging. We do not have memory, latency, debugging issues, etc with these...
12
2500
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 When I place this application in IIS it takes 16 seconds for the exact bit of code. I have narrowed the code down to loopoing around the dataset and creating an object for every row in the dataset. My question is why does it take so long under...
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9996
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6674
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3964
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3564
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.