Exemple #1
0
    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 LogFormatDeferredTemplate()
    {
        MemoryWatch watch      = new MemoryWatch();
        var         randomList = GetRandomList();

        Log.EnableLogging = false;

        var listeners = new ListNoAlloc <IEvent>(10);

        var handler = new HandlerEvent();

        listeners.Add(handler);

        watch.Start();

        int l = 0;

        for (int j = 0; j < Seconds; ++j)
        {
            for (int i = 0; i < FramesPerSecond; ++i)
            {
                // This will not actually be written because EnableLogging = false
                // We deferr string construction here
                Log.InfoT("Formatted message from integer", i);
                l += 1;
            }
        }

        watch.Stop();
        results.Add(watch.GetAllocationDesc($"Log message + template int (deferred) that is ignored for each frame at {FramesPerSecond} FPS for {Seconds} s of game"));

        // Just so we force l to be used
        Assert.Less(0, l);
    }
    public void EventsAllocations()
    {
        MemoryWatch watch      = new MemoryWatch();
        var         randomList = GetRandomList();

        OnEvent += EventHandler;

        watch.Start();

        int l = 0;

        for (int j = 0; j < Seconds; ++j)
        {
            for (int i = 0; i < FramesPerSecond; ++i)
            {
                OnEvent += EventHandler;
                OnEvent(true);
                OnEvent -= EventHandler;

                l += 1;
            }
        }

        watch.Stop();
        results.Add(watch.GetAllocationDesc($"Event add/remove for each frame at {FramesPerSecond} FPS for {Seconds} s of game"));

        // Just so we force l to be used
        Assert.Less(0, l);
    }
    public void LambdaAllocation()
    {
        MemoryWatch watch      = new MemoryWatch();
        var         randomList = GetRandomList();

        watch.Start();

        int l = 0;

        for (int j = 0; j < Seconds; ++j)
        {
            for (int i = 0; i < FramesPerSecond; ++i)
            {
                // No context capture!
                Func <int, int> lambda = (int x) => x;
                l += lambda(i);
            }
        }

        watch.Stop();
        results.Add(watch.GetAllocationDesc($"Lambda creation for at {FramesPerSecond} FPS for {Seconds} s of game"));

        // Just so we force l to be used
        Assert.Less(0, l);
    }
    public void LocalClosureAllocation()
    {
        MemoryWatch watch      = new MemoryWatch();
        var         randomList = GetRandomList();

        watch.Start();

        int l  = 0;
        int ii = 0;

        for (int j = 0; j < Seconds; ++j)
        {
            for (int i = 0; i < FramesPerSecond; ++i)
            {
                ii = i;
                l += LocalClosue();
            }
        }

        watch.Stop();
        results.Add(watch.GetAllocationDesc($"Local closure with single capture creation for at {FramesPerSecond} FPS for {Seconds} s of game"));

        // Just so we force l to be used
        Assert.Less(0, l);

        int LocalClosue()
        {
            return(randomList[ii % randomList.Count]);
        }
    }
    public void LocalFunctionAllocation()
    {
        MemoryWatch watch      = new MemoryWatch();
        var         randomList = GetRandomList();

        watch.Start();

        int l = 0;

        for (int j = 0; j < Seconds; ++j)
        {
            for (int i = 0; i < FramesPerSecond; ++i)
            {
                // No context capture!
                l += LocalFunction(i);
            }
        }

        watch.Stop();
        results.Add(watch.GetAllocationDesc($"Local function creation for each frame at {FramesPerSecond} FPS for {Seconds} s of game"));

        // Just so we force l to be used
        Assert.Less(0, l);

        int LocalFunction(int i)
        {
            return(i);
        }
    }
    public void StringNormalConcat()
    {
        MemoryWatch watch = new MemoryWatch();
        var         list  = GetRandomList();

        watch.Start();

        int l = 0;

        for (int j = 0; j < Seconds; ++j)
        {
            for (int i = 0; i < FramesPerSecond; ++i)
            {
                var s = "";

                for (int ii = 0; ii < 10; ++ii)
                {
                    s += list[i].ToString();
                }

                l += s.Length;
            }
        }

        watch.Stop();
        results.Add(watch.GetAllocationDesc($"String combining 10 integers per frame at {FramesPerSecond} FPS for {Seconds} s of game"));

        // Just so we force l to be used
        Assert.Less(0, l);
    }
    public void ReusingController()
    {
        MemoryWatch watch = new MemoryWatch();
        var         list  = GetRandomList();

        watch.Start();

        long l = 0;

        {
            var controller = new Controller();

            for (int j = 0; j < Seconds; ++j)
            {
                for (int i = 0; i < FramesPerSecond; ++i)
                {
                    controller.Reuse(i);
                    l += controller.Sum;
                }
            }
        }

        watch.Stop();
        results.Add(watch.GetAllocationDesc($"Reusing a 24 byte controller each frame at {FramesPerSecond} FPS for {Seconds} s of game"));

        // Just so we force l to be used
        Assert.Less(0, l);
    }
    public void Coroutine()
    {
        MemoryWatch watch = new MemoryWatch();
        var         list  = GetRandomList();

        watch.Start();

        int l = 0;

        for (int j = 0; j < Seconds; ++j)
        {
            for (int i = 0; i < FramesPerSecond; ++i)
            {
                // I needed to add an argument or it was optimized out
                var coroutine = SimpleCoroutine(i);
                while (coroutine.MoveNext())
                {
                    if (coroutine.Current == null)
                    {
                        l += 1;
                    }
                }
            }
        }

        watch.Stop();
        results.Add(watch.GetAllocationDesc($"Coroutine start per frame at {FramesPerSecond} FPS for {Seconds} s of game"));

        // Just so we force l to be used
        Assert.Less(0, l);
    }
    public void StringBuilderReuse()
    {
        MemoryWatch watch = new MemoryWatch();
        var         list  = GetRandomList();

        watch.Start();
        var builder = new StringBuilder();
        int l       = 0;

        for (int j = 0; j < Seconds; ++j)
        {
            for (int i = 0; i < FramesPerSecond; ++i)
            {
                builder.Clear();

                for (int ii = 0; ii < 10; ++ii)
                {
                    builder.Append(list[i]);
                }

                // We need string at the end
                var s = builder.ToString();
                l += s.Length;
            }
        }

        watch.Stop();
        results.Add(watch.GetAllocationDesc($"Builder reuse clear combining 10 integers per frame at {FramesPerSecond} FPS for {Seconds} s of game"));

        // Just so we force l to be used
        Assert.Less(0, l);
    }
Exemple #11
0
    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 ForeachLoopArray()
    {
        MemoryWatch watch = new MemoryWatch();
        var         list  = GetRandomList().ToArray();

        watch.Start();

        long l = 0;

        for (int j = 0; j < Seconds; ++j)
        {
            for (int i = 0; i < FramesPerSecond; ++i)
            {
                foreach (var x in list)
                {
                    l += x;
                }
            }
        }

        watch.Stop();
        results.Add(watch.GetAllocationDesc($"For each per frame on array at {FramesPerSecond} FPS for {Seconds} s of game"));

        // Just so we force l to be used
        Assert.Less(0, l);
    }
    public void ForeachLambdaAllocation()
    {
        MemoryWatch watch     = new MemoryWatch();
        var         delegates = new List <IDelegate>();

        delegates.Add(new DelegateListener());
        delegates.Add(new DelegateListener());

        watch.Start();

        int l = 0;

        for (int j = 0; j < Seconds; ++j)
        {
            for (int i = 0; i < FramesPerSecond; ++i)
            {
                delegates.ForEach(x => x.OnCalled());
                l += i;
            }
        }

        watch.Stop();
        results.Add(watch.GetAllocationDesc($"Foreach lambda allocation for at {FramesPerSecond} FPS for {Seconds} s of game"));

        // Just so we force l to be used
        Assert.Less(0, l);
    }
    public void Dictionary()
    {
        MemoryWatch watch      = new MemoryWatch();
        var         dictionary = new Dictionary <int, string>();

        var handler = new HandlerEvent();

        const string ConstString = "Const string";

        watch.Start();

        int l = 0;

        for (int j = 0; j < Seconds; ++j)
        {
            for (int i = 0; i < FramesPerSecond; ++i)
            {
                // Add or overwrite
                for (int ii = 0; ii < 100; ii++)
                {
                    dictionary[i * 10 + 30 + ii] = ConstString;
                }
                l += 1;
            }
        }

        watch.Stop();
        results.Add(watch.GetAllocationDesc($"Dictionary add/replace 100 elements for each frame at {FramesPerSecond} FPS for {Seconds} s of game"));

        // Just so we force l to be used
        Assert.Less(0, l);
    }
    public void ListNoAllocTest()
    {
        MemoryWatch watch      = new MemoryWatch();
        var         randomList = GetRandomList();

        var listeners = new ListNoAlloc <IEvent> (10);

        var handler = new HandlerEvent();

        listeners.Add(handler);

        watch.Start();

        int l = 0;

        for (int j = 0; j < Seconds; ++j)
        {
            for (int i = 0; i < FramesPerSecond; ++i)
            {
                listeners.Add(handler);
                listeners.ForEach(x => x.EventHandler(true));
                listeners.Remove(handler);

                l += 1;
            }
        }

        watch.Stop();
        results.Add(watch.GetAllocationDesc($"Event add/remove using ListNoAlloc for each frame at {FramesPerSecond} FPS for {Seconds} s of game"));

        // Just so we force l to be used
        Assert.Less(0, l);
    }
Exemple #16
0
    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");
    }
Exemple #17
0
    public void BasicFunctionality()
    {
        InitType();
        DayOfWeek day = DayOfWeek.Monday;

        MemoryWatch watch  = MemoryWatch.Create;
        Value       value  = Value.Create(day);
        DayOfWeek   outDay = value.As <DayOfWeek>();

        watch.Validate();

        Assert.Equal(day, outDay);
        Assert.Equal(typeof(DayOfWeek), value.Type);
    }
Exemple #18
0
    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");
    }
Exemple #19
0
    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);
    }
Exemple #20
0
    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);
    }
Exemple #21
0
    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");
    }
Exemple #22
0
    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);
    }
    public void LinqSelectAndSumFilter()
    {
        MemoryWatch watch = new MemoryWatch();
        var         list  = GetRandomList();

        watch.Start();

        int l = 0;

        for (int j = 0; j < Seconds; ++j)
        {
            for (int i = 0; i < FramesPerSecond; ++i)
            {
                l += list.Select(x => x / 2).Sum();
            }
        }

        watch.Stop();
        results.Add(watch.GetAllocationDesc($"Each frame select + sum on list of length 100 at {FramesPerSecond} FPS for {Seconds} s of game"));

        // Just so we force l to be used
        Assert.Less(0, l);
    }
    public void IntToStringAllocations()
    {
        MemoryWatch watch  = new MemoryWatch();
        var         random = new System.Random();

        watch.Start();

        int l = 0;

        for (int j = 0; j < Seconds; ++j)
        {
            for (int i = 0; i < FramesPerSecond; ++i)
            {
                string s = random.Next().ToString();
                l += s.Length;
            }
        }

        watch.Stop();
        results.Add(watch.GetAllocationDesc($"One Int to string conversion per frame at {FramesPerSecond} FPS for {Seconds} s of game"));

        // Just so we force l to be used
        Assert.Less(0, l);
    }