String splitting to Arrays

BlitzMax Forums/BlitzMax Programming/String splitting to Arrays

Actually, this is weird, mine performs better if you run it first:


Local I:Int
Global time:Int
Global time2:Int
Global time3:Int


Print "Pertubatios :"

time = MilliSecs()
'Local myList:TList
For Local b:Int = 1 To 1000
	Local myList:TList = SplitString("This is a longer test string that I am using to test this split string test thing", " ")
Next
Time2 = MilliSecs()-time
Print "needed time: " + time2


Print "Return_strip  : "
Local Strip2:String[]
time = MilliSecs()
For Local a:Int = 1 To 1000
	Strip2 = Return_Strip("This is a longer test string that I am using to test this split string test thing",32)
Next
Time2 = MilliSecs()-time
Print "needed time: " + time2






Print "Testloop : "
time = MilliSecs()
For i = 1 To 10000000
Next
Time2 = MilliSecs()-time
Print "needed time: " + time2




Function Return_Strip:String[](_String:String,_strip:Int)

	Local Text_Array : String[1]
	Local R_Text:String = _string
	Local i:Int = 0

	Repeat
		If R_Text.Length = 0 Then Exit
			Local sp_p:Int = R_Text.Find(Chr(_strip))
			If sp_p = - 1  Then
				Text_Array[I] = R_Text
				Exit
			End If
			Text_Array[I] = Left(R_Text,sp_p)
			R_Text = Right(R_Text,(R_text.Length - sp_p)-1)
			I:+1
			Text_Array = Text_array[..I+1]
	Forever
	
	Return Text_array	
	
End Function

Function SplitString:TList(inString:String, Delim:String)
	Local tempList : TList = New TList
	Local currentChar : String = ""
	Local count : Int = 0
	Local TokenStart : Int = 0
	
	If Len(Delim)<>1 Then Return Null
	
	inString = Trim(inString)
	
	For count = 0 Until Len(inString)
		If inString[count..count+1] = delim Then
			tempList.AddLast(inString[TokenStart..Count])
			TokenStart = count + 1
		End If
	Next
	tempList.AddLast(inString[TokenStart..Count])	
	Return tempList
End Function




*EDIT*
solved it:


Local I:Int
Local time:Int
Local time2:Int


Print "Pertubatios :"

time = MilliSecs()
'Local myList:TList
For Local b:Int = 1 To 1000
	Local myList:TList = SplitString("This is a longer test string that I am using to test this split string test thing", " ")
	myList= Null
	FlushMem
Next
Time2 = MilliSecs()-time
Print "needed time: " + time2


Print "Return_strip  : "
Local Strip2:String[]
time = MilliSecs()
For Local a:Int = 1 To 1000
	Strip2 = Return_Strip("This is a longer test string that I am using to test this split string test thing",32)
	strip2 = Null
	FlushMem
Next
Time2 = MilliSecs()-time
Print "needed time: " + time2






Print "Testloop : "
time = MilliSecs()
For i = 1 To 10000000
Next
Time2 = MilliSecs()-time
Print "needed time: " + time2




Function Return_Strip:String[](_String:String,_strip:Int)

	Local Text_Array : String[1]
	Local R_Text:String = _string
	Local i:Int = 0

	Repeat
		If R_Text.Length = 0 Then Exit
			Local sp_p:Int = R_Text.Find(Chr(_strip))
			If sp_p = - 1  Then
				Text_Array[I] = R_Text
				Exit
			End If
			Text_Array[I] = Left(R_Text,sp_p)
			R_Text = Right(R_Text,(R_text.Length - sp_p)-1)
			I:+1
			Text_Array = Text_array[..I+1]
	Forever
	
	Return Text_array	
	
End Function

Function SplitString:TList(inString:String, Delim:String)
	Local tempList : TList = New TList
	Local currentChar : String = ""
	Local count : Int = 0
	Local TokenStart : Int = 0
	
	If Len(Delim)<>1 Then Return Null
	
	inString = Trim(inString)
	
	For count = 0 Until Len(inString)
		If inString[count..count+1] = delim Then
			tempList.AddLast(inString[TokenStart..Count])
			TokenStart = count + 1
		End If
	Next
	tempList.AddLast(inString[TokenStart..Count])	
	Return tempList
End Function


Flushmem is your friend.

I get:
Pertubatios :
needed time: 25
Return_strip :
needed time: 15
Testloop :
needed time: 43
in Debug Mode

and:
Pertubatios :
needed time: 18
Return_strip :
needed time: 14
Testloop :
needed time: 11
in Release.

Yours is still faster, but it makes me feel better that mine isn't quite as horrendously slow as I thought.