Should ther be much difference in performance here?

After reading the documention for memoryblocks, I was just wondering which of the two codes should run faster:

[code]
dim m as memBlockString

//create a new memBlockString
m = new MemBlockString

//add the strings into the memBlockString
m.appendString textField1.text
m.appendString textField2.text

//retreive the string
Label1.text = m.getString
[/code], From Xojo Docs on MemoryBlock page

dim m as string

//add the string to itself
m = m + textField1.text
m = m + textField2.text

//retreive the string
Label1.text = m

I am pressuming these codes perform the same task.

Thanks

Best way to find out how faster one or the other is… Try it…

The latter will be faster in this case and more correct. When you get into concatenating many strings, other approaches may be required, but in this case go with what’s simplest.

Each time you alter a “normal” String, a new String is created and the old one is destroyed. That’s an inefficient process and therefore (much) more time consuming.

A memory block doesn’t do this. It basically reuses the same memory block, hence the name MemoryBlockString.

Just test it yourself by performing a lot of string appends. You’ll be surprised about the speed difference!

My experience is memory block is faster if you are doing many strings than s=s + newtext

But I have had better speeds with creating a string array and appending to the array. Then performing a join to retrieve the string.

Thanks for your comments and answers! So string arrays are generally faster than memoryblocks? How do I test the performance? I do not think I have ever used a code profiler. How do I use the code profiler, if that it what you use for debugging performance?

[quote=61879:@Marc van Breemen]Each time you alter a “normal” String, a new String is created and the old one is destroyed. That’s an inefficient process and therefore (much) more time consuming.

A memory block doesn’t do this. It basically reuses the same memory block, hence the name MemoryBlockString.

Just test it yourself by performing a lot of string appends. You’ll be surprised about the speed difference![/quote]

While this is all true, when using a MemoryBlock you need to be very careful handling string encodings correctly.

This may be true only in certain circumstances. I had a specific problem that couldn’t be solved with memory block. Out if bounds exceptions were not avoidable without rewriting a bunch of things in order to accommodate memory block. It just so happens that in that specific case appending a string array was also considerably faster.