Thursday, March 30, 2017

C# String Interpolation Performance

Time for a follow up to my String.Concat vs String.Format Performance post from back in 2014!

I recently found out that string interpolation is not nearly as efficient as I would have thought. I also suspected that it was just doing a string concatenation, but it is actually doing a string format. This leads to a pretty significant performance degradation; the following test runs one million iterations of each.

Number
of Args
Interpolation
Milliseconds
String.Format
Milliseconds
String.Concat
Milliseconds
String Add
Milliseconds
StringBuilder
Milliseconds
2 262 260 19 18 34
3 367 367 25 24 35
4 500 513 31 32 41
5 646 635 67 66 44
6 740 723 79 76 49
7 802 819 86 85 52
8 938 936 97 98 58

So, what's the lesson? Don't use string interpolation in high performance areas (such as your logger)!

Enjoy,
Tom

3 comments:

  1. And never, ever, use String Add unless they are constant strings.

    ReplyDelete
  2. Good day! Would you mind if I share your blog with my facebook group?
    There’s a lot of people that I think would really appreciate your content.
    Please let me know. Cheers

    IB KEYGEN

    ccleaner pro crack

    ReplyDelete
  3. It is helpful for me. It is informative. I think you are the best blogger. so thanks for sharing the informative article. World wide Marketing Agency

    Driver Booster Pro Crack

    Pilotedit Crack

    Download Cracks for PCs

    IDM Crack

    Tomabo MP4 Downloader Pro Crack

    World of Cracks

    ReplyDelete

Real Time Web Analytics