Fastest String Replacement Method?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phil Sandler

    Fastest String Replacement Method?

    Hello,

    What is the fastest/most efficient way of doing string replacement in
    csharp/.net?

    An example would be:

    "Hello, my name is {FirstName}, and I live in the town of {City} with
    my wife {WifeName} and my {NumKids} children."

    I have this working with regular expressions, but I'm wondering if
    that's the best/fastest method. Would writing this in another
    language (say, C++) improve the performance?

    Thanks for any insight.


    Phil

  • Tomas Vera

    #2
    Re: Fastest String Replacement Method?

    Are regular expressions significanly faster than the String.Replace( )
    method?

    Have you tried using some unsafe code in your class?

    Nothing's going to be faster than good ole C !!

    -tomas

    On 26 Feb 2007 07:47:45 -0800, "Phil Sandler" <psandler70@hot mail.com>
    wrote:
    >Hello,
    >
    >What is the fastest/most efficient way of doing string replacement in
    >csharp/.net?
    >
    >An example would be:
    >
    >"Hello, my name is {FirstName}, and I live in the town of {City} with
    >my wife {WifeName} and my {NumKids} children."
    >
    >I have this working with regular expressions, but I'm wondering if
    >that's the best/fastest method. Would writing this in another
    >language (say, C++) improve the performance?
    >
    >Thanks for any insight.
    >
    >
    >Phil

    Comment

    • David Browne

      #3
      Re: Fastest String Replacement Method?



      "Phil Sandler" <psandler70@hot mail.comwrote in message
      news:1172504865 .726410.103110@ q2g2000cwa.goog legroups.com...
      Hello,
      >
      What is the fastest/most efficient way of doing string replacement in
      csharp/.net?
      >
      An example would be:
      >
      "Hello, my name is {FirstName}, and I live in the town of {City} with
      my wife {WifeName} and my {NumKids} children."
      >
      I have this working with regular expressions, but I'm wondering if
      that's the best/fastest method.
      Is is simple? Does it perform well enough for your application?
      >Would writing this in another language (say, C++) improve the performance?
      Probably, but it would be unsafe code. And there are probably faster,
      though more complicated ways to do this safely in managed code. For
      instance you could read through the template word-by-word and build the
      output string as you go with a StringBuilder, replacing the codes as you
      find them.

      David


      Comment

      • =?ISO-8859-1?Q?G=F6ran_Andersson?=

        #4
        Re: Fastest String Replacement Method?

        Phil Sandler wrote:
        Hello,
        >
        What is the fastest/most efficient way of doing string replacement in
        csharp/.net?
        >
        An example would be:
        >
        "Hello, my name is {FirstName}, and I live in the town of {City} with
        my wife {WifeName} and my {NumKids} children."
        >
        I have this working with regular expressions, but I'm wondering if
        that's the best/fastest method. Would writing this in another
        language (say, C++) improve the performance?
        >
        Thanks for any insight.
        >
        >
        Phil
        >
        If you do the replacement using a delegate so that you only do one
        replacement operation, then I think that a regular expression is the
        best method.

        I am sure that you can find a method that performs better under certain
        conditions, but I am also pretty sure that no method that is nearly as
        simple as a Reex replace scales nearly as well.

        If you for example do a string Replace for each keyword you will get
        better results for a short string and a low number of replacements, but
        if the number of replacements is doubled, so is the execution time,
        which is not the case with a Regex Replace.

        Of course you can write an optimised version of what the Regex Replace
        does in this specific case, but that is a lot of work for quite a small
        improvement.

        --
        Göran Andersson
        _____
        Göran Anderssons privata hemsida.

        Comment

        • Ben Voigt

          #5
          Re: Fastest String Replacement Method?


          "Phil Sandler" <psandler70@hot mail.comwrote in message
          news:1172504865 .726410.103110@ q2g2000cwa.goog legroups.com...
          Hello,
          >
          What is the fastest/most efficient way of doing string replacement in
          csharp/.net?
          >
          An example would be:
          >
          "Hello, my name is {FirstName}, and I live in the town of {City} with
          my wife {WifeName} and my {NumKids} children."
          >
          I have this working with regular expressions, but I'm wondering if
          that's the best/fastest method. Would writing this in another
          language (say, C++) improve the performance?
          Depends on whether a particular template is reused with different data... if
          so, it makes a lot of sense to parse it only once, then the parsing speed
          becomes less important than the final encoding. Something like:

          array of literals: { "Hello, my name is ", ", and I live in the town of ",
          .... }
          array of field indexes, negative if reference to literal, positive if
          reference to database field

          will take moderate additional processing to set up, but be very fast to
          execute across a dataset.
          >
          Thanks for any insight.
          >
          >
          Phil
          >

          Comment

          Working...