1

I'm using Visual Studio 2012, 64bit Quad Core Windows 7 OS, .Net framework 4.5.2.

Project is a console application running in Release mode; JIT disabled.

In a C++ environment, there is no slow down on the first iteration. In a C# environment there is substantial slow down. It has been suggested in other similar posts that this is due to Jitting. However, after disabling JIT via Tools->Options->Debugging->Just-in-time (and uncheck the JIT checkboxes) - there is no observable speed up. Below is an example that you can try yourself. Can anyone explain to me how to be "fully compiled" prior to executing the application? This is a low iteration process and I need all iterations to execute quickly.

 private static void Main(string[] args)
        {
            int counter = 10;
            while (counter != 0)
            {
                DoArray();
                counter--;
            }
        }

        public static void DoArray()
        {
            var stopwatch = new Stopwatch();
            int[] set = {0, 1, 9, 10, 11};
            int[] toRemove = {1, 14, 27, 40};
            stopwatch.Start();
            int[] removed = RemoveElementsFromSet(set, toRemove);
            stopwatch.Stop();
            if (removed.Length != 4)
                throw new ApplicationException("ERROR");
            Console.WriteLine("elapsed[ticks]: {0}", stopwatch.ElapsedTicks);
        }

        internal static int[] RemoveElementsFromSet(int[] set, int[] toRemove)
        {
            var removed = new int[set.Length];
            int removedIndex = 0;
            for (int i = 0; i < set.Length; i++)
            {
                if (toRemove.Contains(i))
                    continue;

                removed[removedIndex] = set[i];
                removedIndex++;
            }
            Array.Resize(ref removed, removedIndex);
            return removed;
        }
    }
elapsed[ticks]: 6651
elapsed[ticks]: 7
elapsed[ticks]: 6
elapsed[ticks]: 6
elapsed[ticks]: 5
elapsed[ticks]: 5
elapsed[ticks]: 6
elapsed[ticks]: 5
elapsed[ticks]: 3
elapsed[ticks]: 2
Press any key to continue . . .
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
sapbucket
  • 6,795
  • 15
  • 57
  • 94
  • 5
    `Project is a console application running in Release mode` and `after disabling JIT via Tools->Options->Debugging->Just-in-time` see the problem. You've disabled Just-in-time debugging, not JIT in general, which you can't disable because that's kind of the whole point of the .NET runtime. There are tools that will convert MSIL to native code available if you really want to do that. – Matt Burland Nov 03 '15 at 21:56
  • 1
    Also [see here](https://msdn.microsoft.com/en-us/library/ht8ecch6(v=vs.100).aspx) – Matt Burland Nov 03 '15 at 21:58
  • Why would a C++ .Net not have the same issue? – sapbucket Nov 03 '15 at 21:59
  • And also see [this](https://msdn.microsoft.com/en-us/library/5hs4b7a6.aspx) to understand what just-in-time debugging is. – Matt Burland Nov 03 '15 at 22:00
  • @PetSerAl: okay thank you much, I see what is going on now. Please post to use ngen.exe and I'll provide points for answer. – sapbucket Nov 03 '15 at 22:01
  • @sapbucket: Possibly you are statically compiling C++. I haven't touched C++ for a long time, but I believe you had the option to compile to MSIL or directly to machine code depending on your project. – Matt Burland Nov 03 '15 at 22:09
  • 1
    Based your comments, it's clear you misunderstood the option you were using. In the comments you have posed a new question and asked for it to be answered; you should edit your question if you want to change/elaborate. That said, your new question has already been answered; please see the marked duplicate for details on using ngen.exe. See also [How is .NET JIT compilation performance (including dynamic methods) affected by image debug options of C# compiler?](https://stackoverflow.com/q/10170602) for important information you should be aware of if using ngen.exe – Peter Duniho Nov 04 '15 at 03:19

0 Answers0