String concatenation in C # with interned strings

I know this question has been completed, but I have a slightly different turn. Some of them noted that this is premature optimization, which is quite true if I ask only about practicality and practicality. My problem is rooted in a practical problem, but I'm still interested in it all the same.


I am creating a bunch of SQL statements to create a script (how it will be saved to disk) to recreate the database schema (easily many hundreds of tables, views, etc.). This means that my string concatenation is append-only only. StringBuilder, according to MSDN, works by storing the internal buffer (required char []) and copying string characters into it and redistributing the array as necessary.

However, in my code there are a lot of repeated lines ("CREATE TABLE [", "GO \ n", etc.), which means that I can use them being interned but not if I use StringBuilder, as they will be copied each time. The only variables are, in essence, the names of the tables and those that already exist as rows in other objects that are already in memory.

So, as far as I can tell, after reading my data and creating my objects containing the schema data, all my string information can be reused by interning, right?

Assuming that, wouldn't a List or LinkedList be faster because they store pointers to interned strings? Then this is just one call to String.Concat () to select one chain of the entire string that exactly matches the length.

A List should redistribute the string [] of interned pointers, and the linked list will have to create nodes and modify pointers, so they are not "free", but if I combine many thousands of interned strings , then they seem to be more efficient.

, SQL StringBuilder, char [], , .

, , , :

  •  
  • StringBuilder  
  • < >  
  • LinkedList < >  
  • StringBuilder  
  • - ?

( ) : StreamWriter ? List LinkedList, .

: (.NET 3.5) MSDN. : " , , , , ". char [], , ( ), .

+3
7

Win32

+3

- , StringBuilder ( script). .

( - ):

FileStream f = new FileStream("yourscript.sql");
foreach (Table t in myTables)
{
    f.write("CREATE TABLE [");
    f.write(t.ToString());
    f.write("]");
    ....
}

script .

?

+3

, StringBuilder, . , 20% 30%, . , , .

, , !

EDIT: @Colin Burnett, , , , . .

+2

StringBuilder String . String System, StringBuilder . StringBuilder , . / .

, . , , , , .

, . , "SOMESTRINGA" "SOMESTRINGB" , .

+1

( ) , MIGHT , .

, , perf, , , .

, , . , , , .

+1

A StringBuilder char[] , . , , , StringBuilder .

, StringBuilder , , , 1,33 . StringBuilder, .

, , , . , , StringBuilder ( ), .

+1

++ ? , T/SQL, ++.

- malloc. 4 32- . .

#, - :

string varString1 = tableName;
string varString2 = tableName;

StringBuilder sb1 = new StringBuilder("const expression");
sb1.Append(varString1);

StringBuilder sb2 = new StringBuilder("const expression");
sb2.Append(varString2);

string resultingString = sb1.ToString() + sb2.ToString();

, , .

0

Source: https://habr.com/ru/post/1707491/


All Articles