sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
kelvin.koogan@googlemail.com's Avatar

Fast String operations


Question posted by: kelvin.koogan@googlemail.com (Guest) on August 8th, 2008 01:55 PM
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-
Quote:
Originally Posted by
>Name, pLine->Value, pLine->Desc) +

"\n";

Anyway to make this faster?

TIA,
KK
3 Answers Posted
Jeroen Mostert's Avatar
Jeroen Mostert August 8th, 2008 07:05 PM
Guest - n/a Posts
#2: Re: Fast String operations

Join Bytes! wrote:
Quote:
Originally Posted by
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-
Quote:
Originally Posted by
>Name, pLine->Value, pLine->Desc) +

"\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.

--
J.
Pavel Minaev's Avatar
Guest - n/a Posts
#3: Re: Fast String operations

On Aug 8, 4:45*pm, kelvin.koo...@googlemail.com wrote:
Quote:
Originally Posted by
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
Quote:
Originally Posted by
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.
Ben Voigt [C++ MVP]'s Avatar
Ben Voigt [C++ MVP] September 3rd, 2008 04:55 PM
Guest - n/a Posts
#4: Re: Fast String operations

Pavel Minaev wrote:
Quote:
Originally Posted by
On Aug 8, 4:45 pm, kelvin.koo...@googlemail.com wrote:
Quote:
Originally Posted by
>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
>
Quote:
Originally Posted by
>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.


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.


 
Not the answer you were looking for? Post your question . . .
196,990 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 196,990 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors