Fast String operations

  • Thread starter Thread starter kelvin.koogan
  • Start date Start date
K

kelvin.koogan

Using C++/CLI but I would imagine C# is the same.

What is the fastest to do the following operations

1) Compare two Strings without case-sensitivity.

2) Take a group of objects with 4 string fields and tabulate them, so
each field is in a column, in a single String, one line per object
with \n separating them, calculating the minimum width required for
each column and them formatting the rows so all the fields in each
column line up.

So at the moment I'm
i) Looping over all the objects determining the minimum width of each
column.
ii) Creating a format string with something like String::Format("{{0,-
{0}}}{{1,-{1}}}{{2,-{2}}}{{3}}\n", columnWidth1, columnWidth2,
columnWidth3);
iii) Then doing pLine += String::Format(pFmt, pLine->Type, pLine-
Name, pLine->Value, pLine->Desc) +
"\n";

Anyway to make this faster?

TIA,
KK
 
Using C++/CLI but I would imagine C# is the same.

What is the fastest to do the following operations

1) Compare two Strings without case-sensitivity.

2) Take a group of objects with 4 string fields and tabulate them, so
each field is in a column, in a single String, one line per object
with \n separating them, calculating the minimum width required for
each column and them formatting the rows so all the fields in each
column line up.

So at the moment I'm
i) Looping over all the objects determining the minimum width of each
column.
ii) Creating a format string with something like String::Format("{{0,-
{0}}}{{1,-{1}}}{{2,-{2}}}{{3}}\n", columnWidth1, columnWidth2,
columnWidth3);
iii) Then doing pLine += String::Format(pFmt, pLine->Type, pLine-
"\n";

Anyway to make this faster?
StringBuilder.

Forget optimizing anything else if you're not using StringBuilder; rewrite
it to use that first. Concatenating strings in a loop is hugely inefficient.
See http://msdn.microsoft.com/library/2839d5h5 for examples.
 
Using C++/CLI but I would imagine C# is the same.

What is the fastest to do the following operations

1) Compare two Strings without case-sensitivity.

String.Compare with StringCompare.OrdinalIgnoreCase
2) Take a group of objects with 4 string fields and tabulate them, so
each field is in a column, in a single String, one line per object
with \n separating them, calculating the minimum width required for
each column and them formatting the rows so all the fields in each
column line up.

So at the moment I'm
i) Looping over all the objects determining the minimum width of each
column.
ii) Creating a format string with something like String::Format("{{0,-
{0}}}{{1,-{1}}}{{2,-{2}}}{{3}}\n", columnWidth1, columnWidth2,
columnWidth3);
iii) Then doing pLine += String::Format(pFmt, pLine->Type, pLine->Name,                               pLine->Value, pLine->Desc) +

"\n";

Anyway to make this faster?

Aside from StringBuilder, there doesn't seem to be much more to
optimize here.
 
Pavel said:
String.Compare with StringCompare.OrdinalIgnoreCase


Aside from StringBuilder, there doesn't seem to be much more to
optimize here.

Well, even StringBuilder will grow using that method (although much more
efficiently than looping String::Concat). However as long as you count the
items in (i), you then know the length of each row, so you can easily
compute the needed capacity and perform only one allocation.
 
Back
Top