Skip to content

Instantly share code, notes, and snippets.

@soraphis
Last active April 9, 2019 16:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soraphis/fc90d18f8264e1dcd2ce3a102530b553 to your computer and use it in GitHub Desktop.
Save soraphis/fc90d18f8264e1dcd2ce3a102530b553 to your computer and use it in GitHub Desktop.
Testing garbage allocation of strings and string concatenation in unity
using System.Text;
using NUnit.Framework;
using Unity.Profiling;
namespace Tests
{
public class NewTestScript
{
[Test]
public void NewTestScriptSimplePasses()
{
string oldString, someString, aThirdString;
using (new ProfilerMarker("MySystem.NewString").Auto())
{
someString = "0123456789";
}
using (new ProfilerMarker("MySystem.StringConcat").Auto())
{
oldString = "01234" + "56789";
}
someString = "012345";
using (new ProfilerMarker("MySystem.StringConcat2").Auto())
{
oldString = someString + someString.Substring(0, 4);
}
someString = "01234";
oldString = "56789";
using (new ProfilerMarker("MySystem.StringConcat3").Auto())
{
aThirdString = someString + oldString;
}
StringBuilder builder = new StringBuilder(64); // make sure its large enough
using (new ProfilerMarker("MySystem.BuilderAppend").Auto())
{
builder.Append("0123456789");
}
using (new ProfilerMarker("MySystem.BuilderAppend2").Auto())
{
builder.Append("0123456789");
}
using (new ProfilerMarker("MySystem.BuilderAppendLine").Auto())
{
builder.AppendLine("0123456789");
}
using (new ProfilerMarker("MySystem.BuilderClear").Auto())
{
builder.Clear();
}
using (new ProfilerMarker("MySystem.BuilderAppendAC").Auto())
{
builder.Append("0123456789");
}
using (new ProfilerMarker("MySystem.BuilderAppend2AC").Auto())
{
builder.Append("0123456789");
}
using (new ProfilerMarker("MySystem.BuilderAppendLineAC").Auto())
{
builder.AppendLine("0123456789");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment