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 was executed 8 (!!!) times longer). 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 did not work (VS 2013).
In general, looking for advice on profiles in Visual Studio. How to "enable optimization"?
Sharpe 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"); } }
astry direct castes. BoxedInt i = other as BoxedInt; -> var i = (BoxedInt) other; - VeikedoDateTime.Nowin C # and most likelySystem.currentTimeMillis()gives completely left-handed results. In C #,Stopwatch.StartNew()should be used (this significantly changes the measurement results in this example). And it is worth looking for its counterpart in Java. - PashaPash ♦