Performance Int32.CompareTo (int x)

Are there the following performance issues (e.g., box execution)?

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

Additional Information. The application should be soft in real time, so using C # is perhaps a weird choice. However, this is out of my hands.

+5
source share
2 answers

There would be no box with what you have, since Int32 defines two overloads for CompareTo , which accepts intand one that accepts object. In the above example, the first will be called. If the latter needs to be called, then boxing will happen.

+5
source

: BOX NO BOX!

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

BOX NO BOX? IL:

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

NO BOX. ToString() - virtual object, int. struct , , int, int ToString().


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

BOX NO BOX? 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() virtual object, int. , .


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

BOX NO BOX? 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. , ToString(IFormattable) IFormatProvider, int. , , .


, :

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

, CompareTo(int) IComparable<int>, : BOX NO BOX?

+10

All Articles