Fast String operations

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kelvin.koogan@googlemail.com

    Fast String operations

    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
  • Jeroen Mostert

    #2
    Re: Fast String operations

    kelvin.koogan@g ooglemail.com wrote:
    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?
    >
    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.

    Comment

    • Pavel Minaev

      #3
      Re: Fast String operations

      On Aug 8, 4:45 pm, kelvin.koo...@g ooglemail.com wrote:
      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.O rdinalIgnoreCas e
      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.

      Comment

      • Ben Voigt [C++ MVP]

        #4
        Re: Fast String operations

        Pavel Minaev wrote:
        On Aug 8, 4:45 pm, kelvin.koo...@g ooglemail.com wrote:
        >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.O rdinalIgnoreCas e
        >
        >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.


        Comment

        Working...