Custom compare vs Overridden Compare

BlitzMax Forums/BlitzMax Programming/Custom compare vs Overridden Compare

TomToad(Posted 2010) [#1]
I did some tests to see which was better, overriding compare or using a custom compare method. Here is the test code I used.

My tests show that in release mode, the speed is about the same with the overridden compare slightly slower than the custom function. In debug mode, the custom function was about 10% faster than the overridden method.

After studying the source for TList, I discovered why. When you don't specify a custom function when calling TList.Sort(), then the sort method will call it's own custom function called CompareObjects(). This function will in turn call the Compare() method in the object
Function CompareObjects( o1:Object,o2:Object )
	Return o1.Compare( o2 )
End Function

So when you override Compare, the program is calling a function which in turn is calling a method, creating a little extra overhead; whereas calling a custom function directly eliminates an extra function call.

This kind of surprises me since I've read in a few posts that an overridden compare is much faster than supplying a custom function, but i can't think that could be possible since a function will be called whether you supply one or not.