public IEnumerator Q10() { MemoryWatch watch = new MemoryWatch(); int sum = 0; watch.Start(); for (int i = 0; i < 100; ++i) { Task t = Sum(i); yield return(null); } watch.Stop(); watch.AssertSize("local func"); Assert.AreNotEqual(0, sum); async Task Sum(int x) { await Task.Yield(); // Will always be continued on Unity thread due to scheduler sum += x; } }
public void Q9() { MemoryWatch watch = new MemoryWatch(); int sum = 0; var list = new List <int>() { 1, 4, 5, 2, 12 }; watch.Start(); for (int i = 0; i < Iterations; ++i) { list.ForEach(x => Sum(x)); } watch.Stop(); watch.AssertSize("lambda + local func"); Assert.AreNotEqual(0, sum); void Sum(int x) { sum += x + list[x % list.Count]; } }
public void Q2() { MemoryWatch watch = new MemoryWatch(); watch.Start(); for (int i = 0; i < Iterations; ++i) { B b = new B(); } watch.Stop(); watch.AssertSize("struct"); }
public void Q1() { MemoryWatch watch = new MemoryWatch(); watch.Start(); for (int i = 0; i < Iterations; ++i) { A a = new A(); } watch.Stop(); watch.AssertSize("class"); }
public void Q7() { MemoryWatch watch = new MemoryWatch(); int sum = 0; watch.Start(); for (int i = 0; i < Iterations; ++i) { string s = $"Iteration {i}"; sum += s.Length; } watch.Stop(); watch.AssertSize("string"); Assert.AreNotEqual(0, sum); }
public void Q8() { MemoryWatch watch = new MemoryWatch(); int sum = 0; watch.Start(); for (int i = 0; i < Iterations; ++i) { var builder = new StringBuilder(); builder.Append("Iterations "); builder.Append(i); sum += builder.Length; } watch.Stop(); watch.AssertSize("builder"); Assert.AreNotEqual(0, sum); }
public void Q4() { MemoryWatch watch = new MemoryWatch(); var list = new List <int>() { 1, 4, 5, 2, 12 }; watch.Start(); for (int i = 0; i < Iterations; ++i) { list.ForEach(x => f(x)); } watch.Stop(); watch.AssertSize("lambda 2"); }
public void Q6() { MemoryWatch watch = new MemoryWatch(); int sum = 0; var list = new List <int>() { 1, 4, 5, 2, 12 }; watch.Start(); for (int i = 0; i < Iterations; ++i) { list.ForEach(x => { sum += (x + i); }); } watch.Stop(); watch.AssertSize("closure 2"); Assert.AreNotEqual(0, sum); }