86

My copy of VS2013 Ultimate compiles this code for 60+ seconds:

class Program
{
    static void Main(string[] args)
    {
        double dichotomy = Dichotomy(
            d =>
            {
                try
                {
                    int size = (int) d;
                    byte[] b = new byte[size];
                    return -b.Length;
                }
                catch (Exception)
                {
                    return 0;
                }
            },
            0,
            int.MaxValue,
            1);

        Console.WriteLine(dichotomy);
        Console.ReadKey();
    }

    private static double Dichotomy(
        Func<double, double> func,
        double a,
        double b,
        double epsilon)
    {
        double delta = epsilon / 10;
        while (b - a >= epsilon)
        {
            double middle = (a + b) / 2;
            double lambda = middle - delta, mu = middle + delta;
            if (func(lambda) < func(mu))
                b = mu;
            else
                a = lambda;
        }
        return (a + b) / 2;
    }
}

But if I replace double with int, it compiles immediately. How can be it explained...?

6
  • Compiles immediately on my machine, for both data types... What machine are you compiling it on? Jun 7, 2014 at 16:33
  • 1
    Scratch my first comment; I'm seeing the same behavior. ~15 seconds with double and instant with int. 3.4Ghz machine. Jun 7, 2014 at 16:35
  • Interesting. I checked my version and I'm actually running VS2013 Premium - thought I'd got Ultimate installed. Perhaps it's just the Ultimate version that this occurs with. Jun 7, 2014 at 16:44
  • 1
    @chris Just to support that hypothesis, VS Express 2013/Windows Desktop compiles it just fine.
    – ClickRick
    Jun 7, 2014 at 17:11
  • 5
    From what I've heard, "VS2013 very strange behaviour" is hardly an oddity. :) Jun 8, 2014 at 5:00

2 Answers 2

140

I repro, 27 seconds on my machine. The evil-doer is MsMpEng.exe, it burns 100% core for that long. Easy to see in Task Manager's Processes tab.

This is the Windows Defender service, the one that actually performs the malware scans. Disabling it by unticking the "Turn on real-time protection" option instantly fixes the delay. So does adding the path where I store projects to the "Excluded file locations" box, probably your preferred approach.

I'd hate to have to guess at the underlying reason, but have to assume that your source code is triggering a malware rule. Not a great explanation, I don't see the delay when I target a .NET version < 4.0. Okay, I give up :)

2
  • 4
    Omg, Microsoft, you're kidding me... Tnx for help, it's truly MSSE and .Net 4.0+ who are culprits Jun 7, 2014 at 17:07
  • 3
    Good catch! I'm wondering what exactly cause the problem (especially for a program which is so simple and contains almost not external dependencies). Would it be possible that result MSIL bytes from compilation would looks exactly like a pattern of a know malware and thus MsMpEnd would fire up ?
    – tigrou
    Jun 7, 2014 at 20:13
-1

I can't say authoritatively because it's been 20+ years since I fiddled at the assembly code level, but I can easily believe this.

The difference between IEEE standard floating point ops and the ones implemented by a processor often forces linking in library routines to do the translation, while integer math can just use the CPU instructions. At the time the IEEE defined the standard, they made some choices that were very uncommon in implementation, and especially that long ago much more expensive to implement in microcode, and of course the current PC systems are built around chips descendent from the 80387 and 80486, which predate the standard.

So if I'm right, the increased time is because it involves adding a chunk of library code to the link, and linking is a big part of the build time that tends to grow multiplicatively as relocatable chunks are added.

Clang on Linux might or might not have the same slowdown; if it does avoid it, and extending my guesswork even further, that would be an artifact of the omnipresent shared-memory libc you get there and linker optimizations around that.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.