Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code is behaving differently in Release vs Debug Mode

Tags:

c#

.net

release

We have some unit tests that fail when run in Release mode vs debug mode. If I attach a debugger in release mode the tests pass. There is way too much code to publish here so I am really just looking for best practices in debugging Release mode issues. I have checked for:

  • DEBUG and RELEASE preprocessor directives but I did not find any.
  • Conditional Methods

SOLUTION: In this case it is because I was comparing floating point variables for equality. I could not change the floats to decimal without a major refactoring so I added an extension method:

public static class FloatExtension
{
    public static bool AlmostEquals(this float f1, float f2, float precision)
    {
        return (Math.Abs(f1 - f2) <= precision);
    }

    public static bool AlmostEquals(this float f1, float f2)
    {
        return AlmostEquals(f1, f2, .00001f);
    }

    public static bool AlmostEquals(this float? f1, float? f2)
    {
        if (f1.HasValue && f2.HasValue)
        {
            return AlmostEquals(f1.Value, f2.Value);
        }
        else if (f1 == null && f2 == null)
        {
            return true;
        }
        return false;
    }
}
like image 282
Shaun Bowe Avatar asked Jan 24 '11 20:01

Shaun Bowe


People also ask

What is difference between release and debug mode?

By default, Debug includes debug information in the compiled files (allowing easy debugging) while Release usually has optimizations enabled. As far as conditional compilation goes, they each define different symbols that can be checked in your program, but they are language-specific macros.

Is release mode faster than debug?

Debug Mode: In debug mode the application will be slow. Release Mode: In release mode the application will be faster.

Is debug slower than release?

Release mode is way more slower than debug mode for user defined functions.

How much faster is release than debug?

But the reality is debug build is much faster than release build. The release build normally takes more than 0.30ms, while the debug build takes under 0.3.


2 Answers

One thing that might cause the behaviour that you are seeing is an error that causes a race condition. Attaching a debugger can change the timing of the code such that the race condition is no longer triggered.

To fix it, use synchronization appropriately whenever you have multiple threads accessing data.


I am comparing some float values in the IsEqual method.

That sounds like a very bad idea. You should not compare floats for equality because floating point calcualtions are not 100% precise and you can get representation and rounding errors. Compare to see whether they are sufficiently close together. For calculations involving money, you probably want to use the decimal type instead.

like image 155
Mark Byers Avatar answered Oct 06 '22 01:10

Mark Byers


Since it seems to be floating point related there are so many things that can go wrong. See: C# - Inconsistent math operation result on 32-bit and 64-bit and Double precision problems on .NET

There are so many things that can be trashed with floating points. And comparing floats for equality is a general no-no. You chould check the difference smaller than a reasonably epsilon.

like image 20
stefan Avatar answered Oct 05 '22 23:10

stefan