Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Question on string interning performance

I'm curious. The scenario is a web app/site with e.g. 100's of concurrent connections and many (20?) page loads per second.

If the app needs to server a formatted string

string.Format("Hello, {0}", username);

Will the "Hello, {0}" be interned? Or would it only be interned with

string hello = "Hello, {0}";
string.Format(hello, username);

Which, with regard to interning, would give better performance: the above or,

StringBuilder builder = new StringBuilder()
builder.Append("Hello, ");
builder.Append(username);

or even

string hello = "Hello, {0}";
StringBuilder builder = new StringBuilder()
builder.Append("Hello, ");
builder.Append(username);

So my main questions are: 1) Will a string.Format literal be interned 2) Is it worth setting a variable name for a stringbuilder for a quick lookup, or 3) Is the lookup itself quite heavy (if #1 above is a no)

I realise this would probably result in minuscule gains, but as I said I am curious.

like image 564
Dave Archer Avatar asked Apr 11 '26 01:04

Dave Archer


1 Answers

There is a static method String.IsInterned(str) method. You could do some testing and find out!

http://msdn.microsoft.com/en-us/library/system.string.isinterned.aspx

like image 168
John Buchanan Avatar answered Apr 12 '26 14:04

John Buchanan