正如其他人提到你正在支付拳擊/拆箱罰款,如果可能你應該嘗試消除它。
至於方法的主體,你應該擺脫字典,並使用鏈如果if elses
- 它應該給你一個可衡量的性能提高。
編輯
後霍根的評論我測試過它,讓我吃驚的差異字典+抓之間VS 如果小於5%之間-15%(如果是稍微快一點)在我下面的不完美測試中。
Dict+case: 987.0945ms
Ifs: 937.5104ms
Hogan's array of funcs: 854.4887ms
測試:
class Program
{
static Dictionary<Type, int> TypeDefs = new Dictionary<Type, int>()
{
{typeof(Int16), 1},
{typeof(Int32), 2},
{typeof(Int64), 3},
{typeof(IntPtr), 4},
{typeof(char), 5},
{typeof(String), 6}
};
static KeyValuePair<Type,object>[] _Types = new[]
{ new KeyValuePair<Type,object> (typeof(Int16),5),
new KeyValuePair<Type,object> (typeof(Int32),57),
new KeyValuePair<Type,object> (typeof(Int64),157),
new KeyValuePair<Type,object> (typeof(IntPtr),new IntPtr(6)),
new KeyValuePair<Type,object> (typeof(String),"Hello!"),
};
public static object ConvertFromDBValue(Type type, object value)
{
try
{
switch (TypeDefs[type])
{
case 1: // {typeof(Int16), 1},
{
return Convert.ToInt16(value);
}
case 2: // {typeof(Int32), 2},
{
return Convert.ToInt32(value);
}
case 3: // {typeof(Int64), 3},
{
return Convert.ToInt64(value);
}
case 4: // {typeof(IntPtr), 4},
{
return value;
}
case 5: // {typeof(Char), 17},
case 6: // {typeof(String), 18},
{
return value;
}
default:
{
return value;
}
}
}
catch (Exception ex)
{
throw ex;
}
}
public static object ConvertFromDBValue2(Type type, object value)
{
try
{
if (type == typeof(Int16))
{
return Convert.ToInt16(value);
}
if (type == typeof(Int32))
{
return Convert.ToInt32(value);
}
if (type == typeof(Int64))
{
return Convert.ToInt64(value);
}
if (type == typeof(IntPtr))
{
return (IntPtr)value;
}
if (type == typeof(Char) || type == typeof(String))
{
return value.ToString().Trim();
}
return value;
}
catch (Exception ex)
{
throw ex;
}
}
static Func<object, object>[] funcList =
{
(value) => value, //0
(value) => Convert.ToInt16(value), //1
(value) => Convert.ToInt32(value), //2
(value) => Convert.ToInt64(value), //3
(value) => value, //4
(value) => value, //5
(value) => value, //6
(value) => value, //7
(value) => value, //8
(value) => value, //9
(value) => value, //10
(value) => value, //11
(value) => value, //12
(value) => value, //13
(value) => value, //14
(value) => value, //15
(value) => value, //16
(value) => value, //17
(value) => value.ToString().Trim() //18
};
public static object ConvertFromDBValueHogan(Type type, object value)
{
return funcList[TypeDefs[type]](value);
}
static void Main(string[] args)
{
var sw = new System.Diagnostics.Stopwatch();
Random random = new Random(113453113);
sw.Start();
for (int i = 0; i < 10000000; i++)
{
var x = random.Next(5);
var testValue = _Types[x];
var p = ConvertFromDBValue(testValue.Key, testValue.Value);
}
var elapsed = sw.Elapsed;
Console.WriteLine($"Dict+case: {elapsed.TotalMilliseconds}ms");
sw.Restart();
for (int i = 0; i < 10000000; i++)
{
var x = random.Next(5);
var testValue = _Types[x];
var p2 = ConvertFromDBValue2(testValue.Key, testValue.Value);
}
elapsed = sw.Elapsed;
Console.WriteLine($"Ifs: {elapsed.TotalMilliseconds}ms");
sw.Restart();
for (int i = 0; i < 10000000; i++)
{
var x = random.Next(5);
var testValue = _Types[x];
var p3 = ConvertFromDBValueHogan(testValue.Key, testValue.Value);
}
elapsed = sw.Elapsed;
Console.WriteLine($"Hogan's array of funcs: {elapsed.TotalMilliseconds}ms");
Console.ReadLine();
}
}
我懷疑它的'之開關,減慢你失望。我懷疑這是需要時間的價值的拳擊。如果你的字典是強類型的,那麼它將工作得非常快。 –
交換機應該是很快的。這取決於。你稱這種方法有多少次?不要一遍又一遍地使用try-catch,因爲這太慢了。 –
BTW :(與你的性能問題無關)像Dictionary>這樣的字典可以是一個簡潔的解決方案,它可以避免使用* switch * .... **'Dictionary > TypeDefs = new Dictionary >(){typeof(Int16),x => Convert.ToInt16(x)}, {typeof(String), x => x.ToString()。Trim()} }; var val = TypeDefs [typeof(short)](「12345」);'** –