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.
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/default.asp?url=/library/en-us/vbcon/html/vbconConditionalCompilationWithTraceDebug.asp
http://msdn.microsoft.com/msdnmag/issues/01/02/Bugslayer/default.aspx
Hope this helps
Jay
"John Smith" <som...@microsoft.com> wrote in message
news:5yNUb.78971$5K1.3...@twister.southeast.rr.com...
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.
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
You can use ILDASM.EXE to see the difference to the IL generated.
Hope this helps
Jay
"John Smith" <som...@microsoft.com> wrote in message
news:E3PUb.78983$5K1.3...@twister.southeast.rr.com...
>>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.
>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.
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.
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" <mattias.don...@mvps.org> wrote in message
news:%23GpIG%23O7DH...@tk2msftngp13.phx.gbl...
Not that I recall, but perhaps your memory is better than mine. :-)
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