Dim t As Text = "Hello, World!"
Dim reverse As Text
dim start as double
while t.Length<30000
t=t+t
wend // creates a string 53,248 characters long
//
// original method
//
start=Microseconds
For Each c As Text In t.Characters
reverse = c + reverse
Next
// OSX = 0.111 seconds
msgbox str((Microseconds-start)/1000000)+":"+str(t.Length)+":"+str(reverse.Length)
//
// New Method
//
dim v() As Text
dim z() As Text
dim i as integer
start=Microseconds
v=t.split
for i=v.Ubound DownTo 0
z.append(v(i))
next
reverse=text.join(z,"")
// OSX= 0.0225 seconds (5x faster)
msgbox str((Microseconds-start)/1000000)+":"+str(t.Length)+":"+str(reverse.Length)
//
Can’t test on Linux, but if OSX got “faster”, then I’d think Linux would too
I suspect the issue is that the first method has to reallocate the string everytime a character is added, where the 2nd method never does
Yep. This is an issue in most high level languages where strings are immutable. It’s why there’s a StringBuilder class in .NET, though you can do the same thing with a string array and join in Xojo.
In my testing on a Mac this is about twice as fast as the split technique:
dim mb as new Xojo.Core.MutableMemoryBlock(Xojo.Core.TextEncoding.ASCII.ConvertTextToData(t))
dim x As Integer = 0
dim y As Integer = mb.Size-1
dim ch As UInt8
while x < y
ch = mb.UInt8Value(x)
mb.UInt8Value(x) = mb.UInt8Value(y)
mb.UInt8Value(y) = ch
x = x + 1
y = y -1
wend
t = Xojo.Core.TextEncoding.ASCII.ConvertDataToText(mb)
However, using an old style MemoryBlock is 7-8x faster:
dim mb As MemoryBlock = t
dim x As Integer = 0
dim y As Integer = mb.Size-1
dim ch As UInt8
while x < y
ch = mb.UInt8Value(x)
mb.UInt8Value(x) = mb.UInt8Value(y)
mb.UInt8Value(y) = ch
x = x + 1
y = y -1
wend
dim s as string = mb.StringValue(0, mb.Size)
s = s.DefineEncoding(Encodings.ASCII)
t = s.ToText
Naturally if you have non-ASCII encoding then you can’t do it this way, though I’m guessing if you convert to/from UTF32 you can get away with 4-byte swaps.
And @Michel Bujardet I have to search the other way around. So I’m doing both. :)[/quote]
This would affect your core logic, but…the absolute fastest and most memory efficient way to do what you need should be to walk the Text as an old style MemoryBlock in both directions. Aside from the initial conversion from Text to MemoryBlock, you’re not wasting any time allocating or swapping memory.
Of course it may not matter if you’re dealing with small blocks of Text.
Hi guys! The feedback is still open, but any chance this has been fixed in the meantime (I’m still on 2017 R2.1) due to a different compiler or something else?