Results 1 to 10 of 10

Thread: Which is faster ?

  1. #1

    Thread Starter
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606

    Which is faster ?

    Imagine I have a variable that has a lot of text in it, let's call it strLargeString, and in it, there's string like {1}, {2}, {3}, etc.. what would be faster?

    VB Code:
    1. For x = 1 to 100
    2.    strLargeString = Replace(strLargeString, "{" & x & "}", "replaced")
    3. Next

    Or

    Loop through the string using InStr to find the first {, then the next } and replace it after and move to the next ?

    The first option, I let VB internal function to do the work, which could be faster, but is always has to start from the beginning. The second option, I keep moving the cursor to towards the end...

    What are your thoughs on this ?

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Which is faster ?

    In a situation like this, I would go with the For/Next.

    Have you tried both ways and done any timings?

  3. #3

    Thread Starter
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606

    Re: Which is faster ?

    Quote Originally Posted by Hack
    Have you tried both ways and done any timings?
    No, not yet...

    If the Replace function could return the location of the string replace, I could do my next replace from there. Unless I do a InStr just before my replace and then I tell the replace function to do it from there ?

  4. #4
    VB Guru ganeshmoorthy's Avatar
    Join Date
    Dec 2005
    Location
    Sharjah, United Arab Emirates
    Posts
    3,031

    Re: Which is faster ?

    if the numbers more than 100?
    If an answer to your question has been helpful, then please, Rate it!

    Have done Projects in Access and Member management systems using BioMetric devices, Smart cards and BarCodes.


  5. #5

    Thread Starter
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606

    Re: Which is faster ?

    Quote Originally Posted by ganeshmoorthy
    if the numbers more than 100?
    Sorry, not sure I get what you are saying ?

    A 100 was just for an example...

  6. #6
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Which is faster ?

    Quote Originally Posted by sebs
    Imagine I have a variable that has a lot of text in it, let's call it strLargeString, and in it, there's string like {1}, {2}, {3}, etc.. what would be faster?

    VB Code:
    1. For x = 1 to 100
    2.    strLargeString = Replace(strLargeString, "{" & x & "}", "replaced")
    3. Next

    Or

    Loop through the string using InStr to find the first {, then the next } and replace it after and move to the next ?

    The first option, I let VB internal function to do the work, which could be faster, but is always has to start from the beginning. The second option, I keep moving the cursor to towards the end...

    What are your thoughs on this ?
    You can find out for yourself. Take a look at the Time code using GetTickCount example in my signature.

  7. #7
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Which is faster ?

    The Replace function has a Start argument, but I don't understand why you need it or why you care where it starts. Replace changes all instances of the found string.

  8. #8
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Which is faster ?

    Bruce, his replace has to be dynamic, since he's replacing a whole lot of different substrings in the string.

  9. #9

    Thread Starter
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606

    Re: Which is faster ?

    Exactly Al42, Bruce, I am thinking sort of like a cursor, I would have something like "text text text{1}text text text{2}text text{3}text text text text" in my string, that's why I was think if it would be better to do my own searching with InStr or do it with Replace. Then I though about the start parameter in the replace, maybe it would be better.

    Thanks Martin, I will look at it.

  10. #10
    Member
    Join Date
    Sep 2005
    Location
    Netherlands, The
    Posts
    41

    Re: Which is faster ?

    using repalce like that is VERY slow, your arguments are not right.
    since evry time it will loop trough the whole string while it has found its match already (count = -1)
    maybe thats why bruce got confused

    try this instead(out of hand) i cant think of anything faster
    VB Code:
    1. dim iX as long
    2. dim iY as long
    3. dim iZ as long
    4.  
    5. iz = len("replaced")
    6. ix = instr(1, strLargeString, "{", vbtextcompare)
    7. do while ix > 0
    8.   iy = iy +1
    9.   strLargeString = Replace(strLargeString, "{" & iy & "}", "replaced", ix, 1, vbtextcompare)
    10.   ix = instr(ix + iz, strLargeString, "{", vbtextcompare)
    11. loop
    Last edited by Mitsukai; Apr 7th, 2006 at 10:24 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width