Skip to content

Instantly share code, notes, and snippets.

@NickCraver
Created November 3, 2015 20:11
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 NickCraver/fce9754c50aa7a4c9593 to your computer and use it in GitHub Desktop.
Save NickCraver/fce9754c50aa7a4c9593 to your computer and use it in GitHub Desktop.
string.Format() vs string.Concat() quick perf tests for Abstractions
string.Concat(): 3011 ms
string.Format(): 18337 ms
string.Concat(): 2885 ms
string.Format(): 18312 ms
string combinePath = "test";
string query = "test2";
string fragment = "test3";
int iterations = 100000000;
string s;
var sw = Stopwatch.StartNew();
for (var i = 0; i < iterations; i++) { s = combinePath + query.ToString() + fragment.ToString(); }
sw.Stop();
Console.WriteLine($"string.Concat(): {sw.ElapsedMilliseconds} ms");
sw.Restart();
for (var i = 0; i < iterations; i++) { s = $"{combinePath}{query.ToString()}{fragment.ToString()}"; }
sw.Stop();
Console.WriteLine($"string.Format(): {sw.ElapsedMilliseconds} ms");
sw.Restart();
for (var i = 0; i < iterations; i++) { s = combinePath + query.ToString() + fragment.ToString(); }
sw.Stop();
Console.WriteLine($"string.Concat(): {sw.ElapsedMilliseconds} ms");
sw.Restart();
for (var i = 0; i < iterations; i++) { s = $"{combinePath}{query.ToString()}{fragment.ToString()}"; }
sw.Stop();
Console.WriteLine($"string.Format(): {sw.ElapsedMilliseconds} ms");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment