Int32.CompareTo(int x) performance
Asked Answered
A

2

9

Does the following have any performance issues (e.g. performing boxing)?

public int CompareIntValues(int left, int right)
{
    return left.CompareTo(right);
}

Some further information. The application is supposed to be soft real time, so using C# is perhaps an odd choice. However, that's out of my hands.

Aeri answered 23/8, 2011 at 14:7 Comment(5)
Are you actually having performance issues with this code? If not, you are micro-optimizing.Meghan
I'm not sure, since CompareTo implements IComparable, but it shouldn't be an issue unless CompareIntValues is called many, many times within a single second.Hezekiah
Agree with Oded, if you are concerned about performance in this context, you should probably be using a lower level language.Piggish
Honestly, I haven't profiled the code yet. However, this is called a large number of times in performance critical code.Aeri
@Jack: Unfortunately that's not my decision, but your point is taken.Aeri
K
7

There wouldn't be any boxing with what you have as Int32 defines two overloads to CompareTo, one that takes an int and one that takes an object. In your example above, the former will be called. If the latter must be called, then boxing would occur.

Kirkwood answered 23/8, 2011 at 14:13 Comment(2)
Apologies for the naive follow up question, but left isn't boxed is it?Aeri
@Aeri - No, it would not be boxed either.Kirkwood
U
13

It's time for everyone's favourite game show: BOX OR NO BOX!

public string DoIntToString(int anInt)
{
    return anInt.ToString();
}

BOX or NO BOX? Let's go to the IL:

IL_0001: ldarga.s anInt
IL_0003: call instance string [mscorlib]System.Int32::ToString()

NO BOX. ToString() is a virtual method on object that is overridden by int. Since structs can't participate in non-interface inheritence, the compiler knows that there are no subclasses of int, and can generate a call to the int version of ToString() directly.


static Type DoIntGetType(int anInt)
{
    return anInt.GetType();
}

BOX or NO BOX? Let's go to the IL:

IL_0001: ldarg.0
IL_0002: box [mscorlib]System.Int32
IL_0007: call instance class [mscorlib]System.Type [mscorlib]System.Object::GetType()

BOX. GetType() is not virtual on object, so there is no int version of the method. The argument has to be boxed, and the call is made on the new boxed object.


private static string DoIntToStringIFormattable(int anInt)
{
    return anInt.ToString(CultureInfo.CurrentCulture);
}

BOX or NO BOX? Let's go to the IL:

IL_0001: ldarga.s anInt
IL_0003: call class [mscorlib]System.Globalization.CultureInfo [mscorlib]System.Globalization.CultureInfo::get_CurrentCulture()
IL_0008: call instance string [mscorlib]System.Int32::ToString(class [mscorlib]System.IFormatProvider)

NO BOX. Even though ToString(IFormattable) is an implementation of the IFormatProvider interface, the call itself is being made directly against the int. For the same reason as the first method, no box is required.


So for the final round, we have your method:

public int CompareIntValues(int left, int right)
{
    return left.CompareTo(right);
}

Knowing that CompareTo(int) is implicit implementation of IComparable<int>, you make the call: BOX or NO BOX?

Unbridle answered 23/8, 2011 at 16:19 Comment(2)
Is there any particular reason why structure types could not implement GetType() as a static function (one definition per structure type) which simply returned the appropriate instance of Type? By my understanding, even something like "return New KeyValuePair<T,U>.GetType()", run within a routine that was generic on T and U, would be a constant, since every different combination of T and U would result in a separate version of that routine being JITted.Horwath
So does it boxing?Germanous
K
7

There wouldn't be any boxing with what you have as Int32 defines two overloads to CompareTo, one that takes an int and one that takes an object. In your example above, the former will be called. If the latter must be called, then boxing would occur.

Kirkwood answered 23/8, 2011 at 14:13 Comment(2)
Apologies for the naive follow up question, but left isn't boxed is it?Aeri
@Aeri - No, it would not be boxed either.Kirkwood

© 2022 - 2024 — McMap. All rights reserved.