枚举ToString和性能

时间:2015-06-23 23:45:00

标签: c# .net enums tostring

我正在分析一些C#代码并最终调查了ToString对枚举值的实现。

ToString方法最终调用System.Type.GetEnumName。这是相关的源代码,取自http://referencesource.microsoft.com/#mscorlib/system/type.cs

public virtual string GetEnumName(object value)
{
    if (value == null)
        throw new ArgumentNullException("value");

    if (!IsEnum)
        throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), "enumType");
    Contract.EndContractBlock();

    Type valueType = value.GetType();

    if (!(valueType.IsEnum || Type.IsIntegerType(valueType)))
        throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnumBaseTypeOrEnum"), "value");

    Array values = GetEnumRawConstantValues();
    int index = BinarySearch(values, value);

    if (index >= 0)
    {
        string[] names = GetEnumNames();
        return names[index];
    }

    return null;
}

// Returns the enum values as an object array.
private Array GetEnumRawConstantValues()
{
    string[] names;
    Array values;
    GetEnumData(out names, out values);
    return values;
}

// This will return enumValues and enumNames sorted by the values.
private void GetEnumData(out string[] enumNames, out Array enumValues)
{
    Contract.Ensures(Contract.ValueAtReturn<String[]>(out enumNames) != null);
    Contract.Ensures(Contract.ValueAtReturn<Array>(out enumValues) != null);

    FieldInfo[] flds = GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);

    object[] values = new object[flds.Length];
    string[] names = new string[flds.Length];

    for (int i = 0; i < flds.Length; i++)
    {
        names[i] = flds[i].Name;
        values[i] = flds[i].GetRawConstantValue();
    }

    // Insertion Sort these values in ascending order.
    // We use this O(n^2) algorithm, but it turns out that most of the time the elements are already in sorted order and
    // the common case performance will be faster than quick sorting this.
    IComparer comparer = Comparer.Default;
    for (int i = 1; i < values.Length; i++)
    {
        int j = i;
        string tempStr = names[i];
        object val = values[i];
        bool exchanged = false;

        // Since the elements are sorted we only need to do one comparision, we keep the check for j inside the loop.
        while (comparer.Compare(values[j - 1], val) > 0)
        {
            names[j] = names[j - 1];
            values[j] = values[j - 1];
            j--;
            exchanged = true;
            if (j == 0)
                break;
        }

        if (exchanged)
        {
            names[j] = tempStr;
            values[j] = val;
        }
    }

    enumNames = names;
    enumValues = values;
}


// Convert everything to ulong then perform a binary search.
private static int BinarySearch(Array array, object value)
{
    ulong[] ulArray = new ulong[array.Length];
    for (int i = 0; i < array.Length; ++i)
        ulArray[i] = Enum.ToUInt64(array.GetValue(i));

    ulong ulValue = Enum.ToUInt64(value);

    return Array.BinarySearch(ulArray, ulValue);
}

如果我正确理解这一点,无论何时请求枚举名称:

  1. 使用插入排序将整个枚举值和名称集插入到数组中。
  2. 将值复制到ulongs数组中。
  3. 使用二进制搜索搜索此数组。
  4. 我认为这是非常低效的吗? (特别是对于有数百或数千个值的枚举)

    似乎某种字典/哈希表结构是理想的。但即使对值进行简单的O(n)线性扫描也可以实现相同的目标,而无需进行排序或二元搜索或分配多余的数组。

    我能够为一个特殊情况预先计算字典,其中ToString在循环中被多次调用;并且取得了显着的性能提升。但是,是否有任何内置的,替代的,更全面的技术,也可以促进JSON序列化等事情?

0 个答案:

没有答案