473,395 Members | 1,440 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,395 software developers and data experts.

[Conditional("DEBUG")] or #if DEBUG

After reading C# documentation the Conditional attribute seemed the way to
go, but after inspecting the IL it seems those methods are still there and I
imagine the CLR removes them. Using #if DEBUG means the code does not even
reach the IL when compiling in release mode.

Is there any benefit to using the Conditional Attribute? Am I right in
thinking there will small performance overhead using [Conditional("DEBUG")]
over #if DEBUG.
Nov 15 '05 #1
10 28573
John,
There is no benefit of using [Conditional("DEBUG")] to the called method
itself per se, the benefit is for the calling methods. You can leave all the
calls to a [Conditional("DEBUG")] method and the compiler will quietly
change them into no ops on release builds, or the actual method calls in
debug builds.

What I will do is use both [Conditional("DEBUG")] & #if DEBUG on internal
methods, which will remove the calls & the code for that method from the
assembly, for public methods in class libraries I will use only
[Conditional("DEBUG")] as assemblies compiled against the class library may
or may not have defined DEBUG.

For an example of the use of [Conditional("DEBUG")], see all the methods in
the System.Diagnostics.Debug & System.Diagnostics.Trace classes. :-)

For more details see:
http://msdn.microsoft.com/library/de...TraceDebug.asp

http://msdn.microsoft.com/msdnmag/is...r/default.aspx

Hope this helps
Jay

"John Smith" <so*****@microsoft.com> wrote in message
news:5y*********************@twister.southeast.rr. com...
After reading C# documentation the Conditional attribute seemed the way to
go, but after inspecting the IL it seems those methods are still there and I imagine the CLR removes them. Using #if DEBUG means the code does not even
reach the IL when compiling in release mode.

Is there any benefit to using the Conditional Attribute? Am I right in
thinking there will small performance overhead using [Conditional("DEBUG")] over #if DEBUG.

Nov 15 '05 #2
Thank you for the clear and concise answer. Public class libraries seem the
reason d'etre for the Conditional Attribute.

Just to clarify when you say the compiler quietly turns them into nops you
mean the JIT compiler, not the compiler than translates C# into IL.
Nov 15 '05 #3
I'd say there's another good reason to use the Conditional attribute:
the code that calls the debug method is much easier to read.

With the traditional approach, you can't just #ifdef out the debug
method because the calls to that method will then fail to compile, so
you have to surround them with #ifdef's as well which introduces a
great deal of clutter in the code.

With the Conditional attribute, you can just leave in all the calls,
and the compiler (don't know which one?) will silently remove them in
Release builds at no loss of runtime speed.
--
http://www.kynosarges.de
Nov 15 '05 #4
John,
Just to clarify when you say the compiler quietly turns them into nops you
mean the JIT compiler, not the compiler than translates C# into IL. No I mean the C# compiler, as the C# compiler already has enough info (DEBUG
is or is not #defined) to decide to include the call or not. A quick test in
VB.NET there were no operations emitted, not even the noop op code.

You can use ILDASM.EXE to see the difference to the IL generated.

Hope this helps
Jay

"John Smith" <so*****@microsoft.com> wrote in message
news:E3*********************@twister.southeast.rr. com... Thank you for the clear and concise answer. Public class libraries seem the reason d'etre for the Conditional Attribute.

Just to clarify when you say the compiler quietly turns them into nops you
mean the JIT compiler, not the compiler than translates C# into IL.

Nov 15 '05 #5
>>the code that calls the debug method is much easier to read.
Agreed. That is why I have been using them.
the compiler (don't know which one?) will silently remove them in
Release builds at no loss of runtime speed.

No loss of runtime speed is what I am not 100% convinced of. The methods
remain in IL so there must be some work -no matter how small- done by the
JIT(??) compiler to skip those methods. This is worrying because I was
calling a Conditional method from BeginRequest in global.asax (an ASP.NET
event which is called for every web page). For now I just converted them to
use the untidy #if statement which decompiling to IL proved has no
performance penalty.
Nov 15 '05 #6
Hi Jay,
What I will do is use both [Conditional("DEBUG")] & #if DEBUG on internal
methods, which will remove the calls & the code for that method from the
assembly,


Are you using #if #endif only around the method body then? Cause you
can't exclude the entire method, since removing conditional method
calls is done after the "preprocessing" step in the compilation
process. The following doesn't compile if DEBUG isn't defined.

#if DEBUG
[Conditional("DEBUG")]
static void Foo() {}
#endif

static void Main()
{
Foo();
}

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 15 '05 #7
No loss of runtime speed is what I am not 100% convinced of. The methods
remain in IL so there must be some work -no matter how small- done by the
JIT(??) compiler to skip those methods.


JIT compilation is done on a method by method basis. If a method is
never called, it will never be JIT compiled. Also, there's nothing for
the JIT compiler to skip, since the calls are removed by the C#
compiler and aren't present in the IL.

So the only way conditional methods can affect performance is that you
have dead code bloating the assembly the methods are in, which may
cause it to take a millisecond or two longer to load.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 15 '05 #8
Mattias,
Yes the method body, as you still need the method itself, for the rest of
the code to compile... I guess I was not as clear as I wanted to be on that
one ;-)

Didn't you and I discuss this once a long time ago. Whoever it was, is why I
add the #if DEBUG to the method body also. Although most of the time I have
the Conditional on public methods, so I don't have a lot using #if DEBUG.

Thanks for clarifying that!
Jay

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:%2******************@tk2msftngp13.phx.gbl...
Hi Jay,
What I will do is use both [Conditional("DEBUG")] & #if DEBUG on internal
methods, which will remove the calls & the code for that method from the
assembly,


Are you using #if #endif only around the method body then? Cause you
can't exclude the entire method, since removing conditional method
calls is done after the "preprocessing" step in the compilation
process. The following doesn't compile if DEBUG isn't defined.

#if DEBUG
[Conditional("DEBUG")]
static void Foo() {}
#endif

static void Main()
{
Foo();
}

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 15 '05 #9
Didn't you and I discuss this once a long time ago.


Not that I recall, but perhaps your memory is better than mine. :-)

Mattias

--
Mattias Sjögren [MVP]
Nov 15 '05 #10
John Smith <so*****@microsoft.com> wrote:
Thank you for the clear and concise answer. Public class libraries seem the
reason d'etre for the Conditional Attribute.

Just to clarify when you say the compiler quietly turns them into nops you
mean the JIT compiler, not the compiler than translates C# into IL.


No. For instance:

using System;
using System.Diagnostics;

class Test
{
static void Main(String[] args)
{
ConditionalTest();
}

[Conditional("CONDITION")]
static void ConditionalTest()
{
}
}

Compiling normally, the IL for Main is:

..method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 1 (0x1)
.maxstack 0
IL_0000: ret
} // end of method Test::Main
Compiling with CONDITION defined, the IL for Main is:

..method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 6 (0x6)
.maxstack 0
IL_0000: call void Test::ConditionalTest()
IL_0005: ret
} // end of method Test::Main

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #11

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

Similar topics

5
by: Harry | last post by:
Hi All, Can anyone clever out there tell me why the below script does not work! - I have a page with two radio boxes with values of "agree" and "not_agree". - The form is set to GET which goes...
3
by: Whiplash | last post by:
I've got a web app that has the debug parameter in the web.config file set to true. Everything works great. However, I want to set debug to false before putting the app into production. When I do...
7
by: What-A-Tool | last post by:
In the following code, "catch" is underlined in blue, and the error "the variable fx is declared but never used" is displayed when I mouse hover over it. Am I doing something wrong, or do I just...
3
by: mosscliffe | last post by:
I need to reference a file on my published server as "../mydir/myfile.txt", which is fine and it works. On my local develop computer the "../mydir" refers to C:\Program Files\Microsoft Visual...
1
by: Marek | last post by:
I use VS2005 with framework 2.0 and I just found a behavior I consider odd. Here is the code that illustrates th eproblem: public static void MethodA() { MethodB() } #if DEBUG
8
by: Bruce | last post by:
I am using VB in Vs2005. Am I missing something or does VB not have the concept of "builds" (release/debug) like in VC? I wrote an assembly and I would like to have a debug version of the DLL...
4
RMWChaos
by: RMWChaos | last post by:
Darnit all, I expect the code I steal from others to work! =D Below is some code that I got to initiate multiple javascripts on page load (rather than using the "onload=" attribute). According the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...

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.