c# – Visual Studio. Project Build Configuration

Question:

I wrote two programs. One in Java, the other in C # with exactly the same content, just to compare performance. The results were amazing. After that, I turned to the teacher to find out what was the matter (the Sharp code took 8 (!!!) times longer to execute). On his machine (VS 2008), he switched from Debug to Release in the studio, after which the program execution time was reduced by 3 times. I came home and the same trick didn't work (VS 2013).

In general, I'm looking for advice on profiles in Visual Studio. How to "enable optimization"?

Sharp code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BoxingTest
{
    public abstract class BoxedVal
    {
        public abstract BoxedVal Add(BoxedVal other);
        public abstract bool LessThan(BoxedVal other);
        public abstract bool GreaterThan(BoxedVal other);
    }

    public class BoxedInt : BoxedVal
    {
        public int Value;

        public BoxedInt(int value)
        {
            Value = value;
        }

        public override BoxedVal Add(BoxedVal other)
        {
            BoxedInt i = other as BoxedInt;
            return new BoxedInt(Value + i.Value);
        }

        public override bool LessThan(BoxedVal other)
        {
            BoxedInt i = other as BoxedInt;
            return Value < i.Value;
        }

        public override bool GreaterThan(BoxedVal other)
        {
            BoxedInt i = other as BoxedInt;
            return Value > i.Value;
        }
    }

    class Program
    {
        private static int Fib(int nn)
        {
            BoxedVal one = new BoxedInt(1);

            BoxedVal a = one;
            BoxedVal b = one;

            BoxedVal n = new BoxedInt(nn);

            BoxedVal thousand = new BoxedInt(1000);

            for (BoxedVal i = new BoxedInt(2); i.LessThan(n); i = i.Add(one))
            {
                BoxedVal c = a.Add(b);
                a = b;
                b = c;

                if (b.GreaterThan(thousand))
                {
                    a = one;
                    b = one;
                }
            }
            return (b as BoxedInt).Value;
        }

        static void Main(string[] args)
        {
            int times = 5;
            int n = 20000000;

            int total = 0;

            for (int i = 0; i < times; i++)
            {
                var start = DateTime.Now;
                int val = Fib(n);

                int dt = (DateTime.Now - start).Milliseconds;
                total += dt;
                Console.WriteLine(val);
                Console.WriteLine("Elapsed: {0} ms", dt);
            }

            Console.WriteLine("Average: {0} ms", total / times);

            Console.ReadLine();

        }
    }
}

Java code:

abstract class BoxedVal
{
    public abstract BoxedVal Add(BoxedVal other);
    public abstract boolean LessThan(BoxedVal other);
    public abstract boolean GreaterThan(BoxedVal other);
}


class BoxedInt extends BoxedVal
{
    public int Value;


    public BoxedInt(int value)
    {
        Value = value;
    }

    @Override
    public BoxedVal Add(BoxedVal other)
    {
        BoxedInt i = (BoxedInt)other;
        return new BoxedInt(Value + i.Value);
    }

    @Override
    public boolean LessThan(BoxedVal other)
    {
        BoxedInt i = (BoxedInt)other;
        return Value < i.Value;
    }

    @Override
    public boolean GreaterThan(BoxedVal other)
    {
        BoxedInt i = (BoxedInt)other;
        return Value > i.Value;
    }
}

public class BoxingTest {

    private static int Fib(int nn)
    {
        BoxedVal one = new BoxedInt(1);

        BoxedVal a = one;
        BoxedVal b = one;

        BoxedVal n = new BoxedInt(nn);

        BoxedVal thousand = new BoxedInt(1000);

        for (BoxedVal i = new BoxedInt(2); i.LessThan(n); i = i.Add(one))
        {
            BoxedVal c = a.Add(b);
            a = b;
            b = c;

            if (b.GreaterThan(thousand))
            {
                a = one;
                b = one;
            }
        }
        return ((BoxedInt)b).Value;
    }

    public static void main(String[] args) {

        int times = 5;
        int n = 20000000;

        long total = 0;


        for (int i = 0; i<times;i++){
            long start = System.currentTimeMillis();

            int val = Fib(n);
            long dt = System.currentTimeMillis() - start;
            total+= dt;
            System.out.println(val);
            System.out.println("Elapsed: "+dt+" ms");
        }

        System.out.println("Average: "+(total/times)+" ms");

    }
}

Answer:

Most likely, you start your application with the F5 button – Start Debugging, and your teacher Ctrl + F5 – Start Without Debugging.

I have C # code running the following time

  • Debug, with debugger: 756ms
  • Debug, no debugger: 989ms
  • Release, with debugger: 844ms
  • Release, no debugger: 388ms

Java code is executed

  • Release (Run): 159ms
  • Debug: 174ms

UPDATE : Funny, increased the constant to 200,000,000 (10 times).

Now C # "overtakes": Average: 863 ms

Java: Average: 1296 ms

Scroll to Top