when using a StringBuilder, does something like this defeat the object?
stringBuilder.Append("Hello " + myName + ", how you doing?");
i.e. should I not be concenating the string and should I be doing this instead? or is there no memory difference?
stringBuilder.Append("Hello ");
stringBuilder.Append(myName
stringBuilder.Append(", how you doing?");
I don't think it's a question of is it more memory.
I think it's a question of repetitive memory allocations and putting that on the stack.
I recently finished an application that parsed XML, and wrote a binary file. It took a second to run on a 3.2 gigahetz machine. It had the option of writing the output to a text file that a human could read. It used basic string concatention and I was astounded to see that the excution time went up to 2 hours.
Stringbuilder precludes the string memory allocation time.
so going back to my original question, which is better
hi can anyone answer my original question with authority? thanks.
For concatenating strings it is faster to use StringBuilder. Thats what its for.
Not to mention it saves alot of keystrokes when it's called in a repeatable function.
Rather than doing
MyString = "This text" & "this text" & me.thiscontrol.text & "this text
Function BuildString(MyString as String) as string
SB.append(MyString)
Return SB
yes, i know that. but if you read my initial post I am asking whether it is ok to concenate a string within a StringBuilder Append method. or is it better to do this within seperate methods??
stringBuilder.AppendFormat("Hello {0}, how you doing?", myName);
or if you had two strings to replace
stringBuilder.AppendFormat("ValueA is: {0} and ValueB is: {1}", ValueA, ValueB);
Its "OK" but still faster to use the string builder.
If your dealing with a strings alot like inside a loop it is alot faster to use string builder but if you have a string your not going to modify maybe once or twice then use the regular string
Yes.:)Wee Bubba wrote:
when using a StringBuilder, does something like this defeat the object?
0 comments:
Post a Comment