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?
Printable View
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?
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
Open File For Output (look in MSDN for specific syntax)
Then Write/Print to the file
Close file.
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!
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?
can anyone help me with this please?
please! now that i've got the funktion please tell me how to use it!
come on! i know you can do it! ;)
Okay hang on. Don't get your knickers in a twist.
I'll take a look at it.
i just found out how to do it using the class file instead!
it works great now! thanks alot! :)
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!
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.
yes...thanks alot for your help!