Skip to content

[Unity 2020.3] ZString's StringBuilder slower than System.StringBuilder? #47

@Lf3T-Hn4D

Description

@Lf3T-Hn4D

When reviewing this wonderful StringBuilder replacement, I noticed the performance for Append() with string type is much worse compared to System.Text.StringBuilder.

image

The test code I'm using is:

int COUNT = 1000;
List<string> strings = new List<string>();
for (int i = 0; i < 100; i++)
	strings.Add("123456789");

Profiler.BeginSample("Append/SharedStringBuilderScope()");
{
	System.Text.StringBuilder sb = new System.Text.StringBuilder();
	for (int i = 0; i < COUNT; i++)
	{
		for (int j = 0; j < strings.Count; j++)
		{
			sb.Append(strings[j]);
		}

		sb.ToString();
		sb.Clear();
	}
}
Profiler.EndSample();

Profiler.BeginSample("Append/ZString.StringBuilder()");
for (int i = 0; i < COUNT; i++)
{
	using (var sb = ZString.CreateStringBuilder())
	{
		for (int j = 0; j < strings.Count; j++)
		{
			sb.Append(strings[j]);
		}

		sb.ToString();
	}
}
Profiler.EndSample();

Is this a known issue? Based on deep profiling in Unity, I noticed that the bottleneck was caused by explicit IntPtr casting. I'm not sure if this is just a bogus timing due to deep profiling, but making IL2CPP builds also shows it to be performing poorly vs System.Text.StringBuilder

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions