#defining out a function in c#?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matthew Caesar

    #1

    #defining out a function in c#?

    I've written some code in the C# programming language. I have a lot of
    calls to a function that prints out some debugging information. I
    would like to occasionally remove all calls to these functions when I
    want the program to run quickly (when I'm measuring how fast it runs)
    but I want the calls to be in when I'm debugging.
    More detail: I have a class called MyDebug with a member called Trace.
    I periodically call MyDebug.Trace(" string with debugging info...").

    Solutions that aren't sufficient:
    - I don't want to have to comment out all calls to this function every
    time I want to do performance analysis, because there are LOTS of
    calls and this would take a long time.
    - I can't simply modify the function to return null instead of
    printing, because the arguments to the function take a lot of overhead
    to compute. For example, I may call MyDebug.Trace(" val1
    "+val1.ToString ()+"val2"+val2. ToString()), which has to concatenate
    several strings together, which causes a lot of overhead. I don't want
    the argument to be evaluated in the first place.

    I suspect there must be a solution for this, since C++ has a nice way
    to solve this:
    (from http://www-subatech.in2p3.fr/~photon...PP-DEB-2.shtml)
    #define assert(THETEST) ((void)0)
    makes it so all calls to assert go away.

    My question then is, how can I remove all calls to a function in C#,
    without commenting the calls out (takes too long) and so that the
    arguments aren't evaluated?

    Thanks a lot for your help.
  • Andre

    #2
    Re: #defining out a function in c#?

    What you need is this: annotate your debugging method with the following:

    [Conditional("DE BUG")]

    Then compile your code with:

    csc program.cs /d:DEBUG

    If you leave out "/d:DEBUG", the function calls to that particular
    method will be ignored. This is a much cleaner than using #defines and #ifs

    -Andre



    John Wood wrote:
    [color=blue]
    > Do you need:
    >
    > #if DEBUG
    > ....
    > #endif
    >
    > ?
    >
    > "Matthew Caesar" <mattccaesar@ya hoo.com> wrote in message
    > news:1bad82df.0 308100807.de4a7 d5@posting.goog le.com...
    >[color=green]
    >>I've written some code in the C# programming language. I have a lot of
    >>calls to a function that prints out some debugging information. I
    >>would like to occasionally remove all calls to these functions when I
    >>want the program to run quickly (when I'm measuring how fast it runs)
    >>but I want the calls to be in when I'm debugging.
    >>More detail: I have a class called MyDebug with a member called Trace.
    >>I periodically call MyDebug.Trace(" string with debugging info...").
    >>
    >>Solutions that aren't sufficient:
    >>- I don't want to have to comment out all calls to this function every
    >>time I want to do performance analysis, because there are LOTS of
    >>calls and this would take a long time.
    >>- I can't simply modify the function to return null instead of
    >>printing, because the arguments to the function take a lot of overhead
    >>to compute. For example, I may call MyDebug.Trace(" val1
    >>"+val1.ToStri ng()+"val2"+val 2.ToString()), which has to concatenate
    >>several strings together, which causes a lot of overhead. I don't want
    >>the argument to be evaluated in the first place.
    >>
    >>I suspect there must be a solution for this, since C++ has a nice way
    >>to solve this:
    >>(from[/color]
    >
    > http://www-subatech.in2p3.fr/~photon...PP-DEB-2.shtml)
    >[color=green]
    >>#define assert(THETEST) ((void)0)
    >>makes it so all calls to assert go away.
    >>
    >>My question then is, how can I remove all calls to a function in C#,
    >>without commenting the calls out (takes too long) and so that the
    >>arguments aren't evaluated?
    >>
    >>Thanks a lot for your help.[/color]
    >
    >
    >[/color]

    Comment

    • Matthew Caesar

      #3
      Re: #defining out a function in c#?

      Nice solution. Thanks a lot!

      Andre <food_crazy@hot mail.com> wrote in message news:<3f367bdc$ 1@clarion.carno .net.au>...[color=blue]
      > What you need is this: annotate your debugging method with the following:
      >
      > [Conditional("DE BUG")]
      >
      > Then compile your code with:
      >
      > csc program.cs /d:DEBUG
      >
      > If you leave out "/d:DEBUG", the function calls to that particular
      > method will be ignored. This is a much cleaner than using #defines and #ifs
      >
      > -Andre
      >
      >[/color]

      Comment

      Working...