|
-
Apr 6th, 2006, 11:06 AM
#1
Thread Starter
Frenzied Member
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:
For x = 1 to 100
strLargeString = Replace(strLargeString, "{" & x & "}", "replaced")
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 ?
-
Apr 6th, 2006, 11:07 AM
#2
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?
-
Apr 6th, 2006, 11:11 AM
#3
Thread Starter
Frenzied Member
Re: Which is faster ?
 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 ?
-
Apr 6th, 2006, 11:31 AM
#4
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.
-
Apr 6th, 2006, 11:33 AM
#5
Thread Starter
Frenzied Member
Re: Which is faster ?
 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...
-
Apr 6th, 2006, 12:19 PM
#6
Re: Which is faster ?
 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:
For x = 1 to 100
strLargeString = Replace(strLargeString, "{" & x & "}", "replaced")
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.
-
Apr 6th, 2006, 12:21 PM
#7
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.
-
Apr 6th, 2006, 08:07 PM
#8
Re: Which is faster ?
Bruce, his replace has to be dynamic, since he's replacing a whole lot of different substrings in the string.
-
Apr 7th, 2006, 09:32 AM
#9
Thread Starter
Frenzied Member
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.
-
Apr 7th, 2006, 10:00 AM
#10
Member
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:
dim iX as long
dim iY as long
dim iZ as long
iz = len("replaced")
ix = instr(1, strLargeString, "{", vbtextcompare)
do while ix > 0
iy = iy +1
strLargeString = Replace(strLargeString, "{" & iy & "}", "replaced", ix, 1, vbtextcompare)
ix = instr(ix + iz, strLargeString, "{", vbtextcompare)
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|