Skip to content

Instantly share code, notes, and snippets.

@pfrozi
Created July 7, 2020 17:47
Show Gist options
  • Save pfrozi/d0bfe928ad9fc14cd8fbc207e45d47e1 to your computer and use it in GitHub Desktop.
Save pfrozi/d0bfe928ad9fc14cd8fbc207e45d47e1 to your computer and use it in GitHub Desktop.
test StringBuilder
int size = 100000;
long start;
List<string> list = new List<string>(size);
for(var i = 0; i < size; i++)
list.Add($"string_{i}");
Console.WriteLine("Aggregate string:");
start = DateTime.Now.Ticks;
list.Aggregate("", (a, b) => a + "," + b);
Console.WriteLine(new TimeSpan(DateTime.Now.Ticks - start));
Console.WriteLine("Join:");
start = DateTime.Now.Ticks;
string.Join(",", list);
Console.WriteLine(new TimeSpan(DateTime.Now.Ticks - start));
Console.WriteLine("Aggregate StringBuilder:");
start = DateTime.Now.Ticks;
list.Aggregate(new StringBuilder(""), (a, b) => a.Append(",").Append(b));
Console.WriteLine(new TimeSpan(DateTime.Now.Ticks - start));
Console.WriteLine("Aggregate $:");
start = DateTime.Now.Ticks;
list.Aggregate("", (a, b) => $"{a},{b}");
Console.WriteLine(new TimeSpan(DateTime.Now.Ticks - start));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment