|
-
Apr 2nd, 2003, 09:18 AM
#1
Thread Starter
Frenzied Member
slow variables!
when filling a string with alot of chars in a loop, it starts to get really slow around 30000 chars....is there a faster way? i really need like a mb in the string! can i write it directly to the hd?
-
Apr 2nd, 2003, 09:31 AM
#2
Junior Member
When you say filling a string, are you iteratively appending a character to the end of a string or are you placing characters into a fixed length string?
If you're just doing myString = myString & theCharacter then performance will eventually hit the floor. You will be wanting it as a fixed length string and then insert the charcter into the string at the place you want it.
The problem with using myString = myString & theCharacter is that VB will allocate a new string every single time it's executed. Each time the line is executed, VB will have to keep the contents of myString, allocate a new string (of size myString+1), copy the contents of myString into it and then append theCharacter onto it. This new string is assigned to myString and then the original myString is released.
So, as the size of myString goes up, performance goes down.
What is it you're using the string for? 1MB is an awful lot of text ...
If you are looking to write it directly to the hard disk, the "Microsoft Scripting Runtime" component is a nice easy one to use.
Dave
-
Apr 2nd, 2003, 09:32 AM
#3
Open File For Output (look in MSDN for specific syntax)
Then Write/Print to the file
Close file.
-
Apr 2nd, 2003, 09:43 AM
#4
Fanatic Member
You might want to look here .
A disadvantage is that you have to keep track of the length of the string, since it is used as the position for the next append.
I have modified the code as following, so it isn't in a class anymore and that there are no global variables.
VB Code:
Public Const STRING_INCREASE = 50000
Public Sub AppendString(psDest As String, psSrc As String, plPos As Long)
Do While plPos + LenB(psSrc) > LenB(psDest)
psDest = psDest & Space(STRING_INCREASE)
Loop
CopyMemory ByVal StrPtr(psDest) + plPos, ByVal StrPtr(psSrc), LenB(psSrc)
plPos = plPos + LenB(psSrc)
End Sub
(It was also more convenient to increase the string_increase size, which is fixed to 10000 in the example)
The performance boost was great, even for several 100 k!
-
Apr 2nd, 2003, 04:25 PM
#5
Thread Starter
Frenzied Member
im not really sure how to use that code...
in a loop i have this code:
VB Code:
PicString = PicString & Chr(TempRed) & Chr(TempGreen) & Chr(TempBlue)
how can i change this using that code?
-
Apr 2nd, 2003, 04:53 PM
#6
Thread Starter
Frenzied Member
can anyone help me with this please?
-
Apr 2nd, 2003, 09:34 PM
#7
Thread Starter
Frenzied Member
please! now that i've got the funktion please tell me how to use it!
-
Apr 3rd, 2003, 03:10 AM
#8
Thread Starter
Frenzied Member
come on! i know you can do it!
-
Apr 3rd, 2003, 03:47 AM
#9
Retired VBF Adm1nistrator
Okay hang on. Don't get your knickers in a twist.
I'll take a look at it.
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Apr 3rd, 2003, 04:00 AM
#10
Thread Starter
Frenzied Member
i just found out how to do it using the class file instead!
it works great now! thanks alot!
-
Apr 3rd, 2003, 04:13 AM
#11
Thread Starter
Frenzied Member
the finished program can be found from the link under my posts...
it saves pictures in a new fileformat that's ~1kb smaller than bmp
not very useful, but it was fun making it!
-
Apr 3rd, 2003, 04:15 AM
#12
Fanatic Member
Sorry, I didn't visit VBForums the rest of the day. There is too much work to do
Glad to see that the class works.
-
Apr 3rd, 2003, 04:20 AM
#13
Thread Starter
Frenzied Member
yes...thanks alot for your help!
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
|