Skip to content

Instantly share code, notes, and snippets.

@bonzaiferroni
Created October 6, 2018 17:26
Show Gist options
  • Save bonzaiferroni/ffd6dcde3b3dd6ebf9a04522364e6556 to your computer and use it in GitHub Desktop.
Save bonzaiferroni/ffd6dcde3b3dd6ebf9a04522364e6556 to your computer and use it in GitHub Desktop.
Test sources of garbage generation
using System;
using System.Diagnostics;
using System.Reflection;
using System.Text;
using UnityEngine;
namespace Automatika
{
public class TestGarbage : MonoBehaviour
{
private object _object;
private Stopwatch _watch;
private Action _method1;
private Action<int> _method2;
private MethodInfo _method1Info;
private MethodInfo _method2Info;
private object[] _method2Args;
private string _one;
private string _two;
private StringBuilder _sb;
private void Start()
{
_watch = new Stopwatch();
_method1 = TestMethod1;
_method2 = TestMethod2;
_method1Info = GetType().GetMethod("TestMethod1");
_method2Info = GetType().GetMethod("TestMethod2");
_method2Args = new object[1];
_sb = new StringBuilder();
_one = "One";
_two = "Two";
}
public void TestMethod1()
{
}
public void TestMethod2(int arg1)
{
}
private void Update()
{
TypeFromGetType();
TypeFromTypeof();
StringBuilder();
ConcatString();
TemplateString();
LiteralString();
BoxInt();
BoxObject();
BoxMethod();
UnboxInt();
InvokeBoxed();
InvokeMethod1Info();
InvokeMethod2Info();
}
// 0 B
private void TypeFromGetType()
{
var type = _watch.GetType();
}
// 0 B
private void TypeFromTypeof()
{
var type = typeof(Stopwatch);
}
// 50 B
private void StringBuilder()
{
_sb.Clear();
_sb.Append(_one);
_sb.Append(" plus ");
_sb.Append(_two);
var str = _sb.ToString();
}
// 50 B
private void ConcatString()
{
var str = _one + " plus " + _two;
}
// 50 B
private void TemplateString()
{
var str = $"{_one} plus {_two}";
}
// 0 B
private void LiteralString()
{
var str = "One plus Two";
}
// 20 B
private void InvokeMethod2Info()
{
_method2Info.Invoke(this, _method2Args);
}
// 32 B
private void InvokeMethod1Info()
{
_method1Info.Invoke(this, null);
}
// 0 B
private void InvokeBoxed()
{
_method1();
}
// 112 B
private void BoxMethod()
{
_method1 = TestMethod1;
}
// 20 B
private void BoxInt()
{
_object = 1;
}
// 0 B
private void UnboxInt()
{
var myValue = (int) _object;
}
// 0 B
private void BoxObject()
{
_object = _watch;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment