SortList becomes slower as list is bigger..

BlitzMax Forums/BlitzMax Beginners Area/SortList becomes slower as list is bigger..

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.


Yes - array sort is faster...but what in case of User-type? I mean sort some user-type by a specific field...




SuperStrict

SeedRnd MilliSecs()

Type TMyObject
	Field Size:Int
	Field Name:String
	
	Function Create:TMyObject(Size:Int, Name:String)
		Local tempObj:TMyObject = New TMyObject
			If Size < 1 Then Size = 1
			tempObj.Size = Size
			tempObj.Name = Name
		Return tempObj
	End Function
	
	Method Compare:Int(otherObject:Object)
		Local comp:TMyObject = TMyObject(otherObject)
		If comp Then
			If Size > Comp.Size Then 
				Return 1 'if the otherObject is less than this one
			ElseIf Size < Comp.Size Then
				Return -1 'if the otherObject is greater than this one
			Else
				Return 0
			EndIf
		EndIf
		Return 0
	End Method
End Type

Const numItems:Int = 20000
Global list:Object[numItems]
Global StartTime:Int
Global out:String

For Local i:Int = 0 To numItems-1
	Local o:TMyObject = TMyObject.Create( Rand( 1, 100 ) , "Object "+i )
	'list = list[..list.length+1]
	list[i] = o
Next

'don't uncomment these with a large number of items!
'out:+ "~nBefore Sort:~n"
'For Local o2:TMyObject = EachIn list
'	out:+ "Name: "+o2.name + " Size:"+o2.Size+"~n"
'Next
StartTime = MilliSecs()
list.sort()
out:+ "Sort Time: "+((MilliSecs()-StartTime)/1000:Float)+" secs~n"

'out:+ "~nAfter Sort:~n"
'For Local o3:TMyObject = EachIn list
'	out:+ "Name: "+o3.name + " Size:"+o3.Size+"~n"
'Next
Print out