String.Replace faster in C++ then vb?

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

    String.Replace faster in C++ then vb?

    I have an function that replaces some string from a huge text that I run
    very often...
    So, I wanted to speed it up... I was using String and StringBuilder.
    But, I was wandering should same function in c++ be faster?

    I tried to test it and it wasn't...
    why?
    Is there any example of faster functions in VC than in VB? (using c++ or c)


  • Patrice

    #2
    Re: String.Replace faster in C++ then vb?

    It depends what is done behind the scene. It's likely that replacing a
    string is done behind the scene in a similar fashion.

    You could perhaps speed this a bit :
    - make sure you don't allocate new strings (using a StringBuilder looks like
    a good idea)
    - make sure you use what you stricly need (for example if you don't need
    case sensitivity don't use it)
    - you have some algorithm allowing to search/replace several strings
    allowing to scan the string once rather than multiple times. The more
    search/replace pairs you'll have the more benefit you could get (an it could
    be worse for simplest cases).
    - do you need to call this. Can you cache the result (for example if you
    replace in the same string you could keep the position) ? How about not
    having to search replace in the first place (for example by having just
    various chunks of text between which you 'll have the needed text. You have
    then no replacements to do...).
    - if this is inside an xml like string, you could use an xml doucment so
    that you can change a node value before wrtiing down the document
    etc...


    In all cases, the first step would be top make sure this search/replace is
    the real cullprit and you'll likely have some investigatino do do on your
    code...

    --
    Patrice

    "buu" <aha@a.coma écrit dans le message de groupe de discussion :
    1214829882.3916 11@news.vmware. amis.hr...
    I have an function that replaces some string from a huge text that I run
    very often...
    So, I wanted to speed it up... I was using String and StringBuilder.
    But, I was wandering should same function in c++ be faster?
    >
    I tried to test it and it wasn't...
    why?
    Is there any example of faster functions in VC than in VB? (using c++ or
    c)
    >

    Comment

    • =?Utf-8?B?RGF2aWQgQW50b24=?=

      #3
      RE: String.Replace faster in C++ then vb?

      I doubt you'd find any performance difference if you use the same .NET
      classes via C++/CLI - the IL code being executed is either identical or very
      close. To get better performance, use the native C++ string methods.
      --
      Source code converters: Convert between C#, C++, Java, and VB with the most accurate and reliable source code converters

      C++ to C#
      C++ to VB
      C++ to Java
      VB & C# to Java
      Java to VB & C#
      Instant C#: VB to C#
      Instant VB: C# to VB
      Instant C++: VB, C#, or Java to C++/CLI


      "buu" wrote:
      I have an function that replaces some string from a huge text that I run
      very often...
      So, I wanted to speed it up... I was using String and StringBuilder.
      But, I was wandering should same function in c++ be faster?
      >
      I tried to test it and it wasn't...
      why?
      Is there any example of faster functions in VC than in VB? (using c++ or c)
      >
      >
      >

      Comment

      • Brian Muth

        #4
        Re: String.Replace faster in C++ then vb?


        "buu" <aha@a.comwro te in message news:1214829882 .391611@news.vm ware.amis.hr...
        >I have an function that replaces some string from a huge text that I run very often...
        So, I wanted to speed it up... I was using String and StringBuilder.
        But, I was wandering should same function in c++ be faster?
        >
        I tried to test it and it wasn't...
        why?
        Is there any example of faster functions in VC than in VB? (using c++ or c)
        Be sure this isn't a case of "premature optimization". Do you perceive a bottleneck in this section of code? Have you profiled your
        application to determine exactly how much time the string replacement is taking relative to the rest of your code?

        The reason I point this out is that it is a common mistake to assume a section of code is taking "a long time" when in fact there
        may be greater benefit in optimizing other sections of code. Profiling is the only way to be sure you are focusing attention in the
        right area.


        Comment

        • Carl Daniel [VC++ MVP]

          #5
          Re: String.Replace faster in C++ then vb?

          David Anton wrote:
          I doubt you'd find any performance difference if you use the same .NET
          classes via C++/CLI - the IL code being executed is either identical
          or very close. To get better performance, use the native C++ string
          methods. --
          On top of that, most of the implementation of System.String is in fact
          written in C++. The managed class is mostly just a metadata wrapper around
          native methods.

          -cd


          Comment

          • buu

            #6
            Re: String.Replace faster in C++ then vb?

            tnx a lot to all your posts.

            well, the answers are:
            - I have profiled my app. and this 'String replacement' is taking almost 20%
            of cpu (let's say it takes minimum 10%)
            - I also tried other ways like putting indexes (lower & upper) wich parts of
            text should be ignored... there's no opmimization in this part, caus
            determining those indexes is almoust the same like replacing (thus you need
            to take care of those indexes)


            well, only thing for replace that does seems to might be faster is using
            Regex.
            Is it faster?


            Comment

            Working...