Jam*_*oat 3 .net c# performance
在C#中是否存在将int(或任何值类型)存储为对象并将它们转换回int(或值类型)的成本?
基本上我需要创建一个内存表.我可以创建特定于每种可能类型的"列"(因此任何原始值类型,如int,double,string等,以及我希望存储在表中的用户定义的引用类型,如Order),或者我可以简单地将一切存储为对象并在访问表格时将它们转换回正确的类型.
所以我的问题是哪种方法表现更好或两者都相同?
或者我应该为所有值类型坚持使用特定的"列"并将所有用户定义的引用类型存储为对象?
谢谢 - 下面的示例代码 - 静态测试方法显示用法.
public sealed class Columns
{
public static void Test()
{
Columns cols = new Columns(100);
cols.SetInt(0,Int_Column,12345);
int value = cols.GetInt(0,Int_Column);
cols.SetObject(1,Object_Column,12345);
int value2 = (int)cols.GetObject(1,Object_Column);
}
private const int Int_Column = 0;
private const int String_Column = 1;
private const int Object_Column = 2;
private int[] _intCol;
private string[] _stringCol;
private object[] _objCol;
public Columns(int rowCount)
{
_intCol = new int[rowCount];
_stringCol = new string[rowCount];
_objCol = new object[rowCount];
}
public void SetInt(int rowIndex, int colIndex, int value)
{
switch(colIndex)
{
case Int_Column:
_intCol[rowIndex] = value;
break;
default:
throw new Exception("Incorrect column index specified.");
}
}
public int GetInt(int rowIndex, int colIndex)
{
switch(colIndex)
{
case Int_Column:
return _intCol[rowIndex];
default:
throw new Exception("Incorrect column index specified.");
}
}
public void SetString(int rowIndex, int colIndex, string value)
{
switch(colIndex)
{
case String_Column:
_stringCol[rowIndex] = value;
break;
default:
throw new Exception("Incorrect column index specified.");
}
}
public string GetString(int rowIndex, int colIndex)
{
switch(colIndex)
{
case String_Column:
return _stringCol[rowIndex];
default:
throw new Exception("Incorrect column index specified.");
}
}
public void SetObject(int rowIndex, int colIndex, object value)
{
switch(colIndex)
{
case Object_Column:
_objCol[rowIndex] = value;
break;
default:
throw new Exception("Incorrect column index specified.");
}
}
public object GetObject(int rowIndex, int colIndex)
{
switch(colIndex)
{
case Object_Column:
return _objCol[rowIndex];
default:
throw new Exception("Incorrect column index specified.");
}
}
}
Run Code Online (Sandbox Code Playgroud)
这称为拳击,在性能和内存使用方面,成本实际上都很大.如果你想避免装箱并且想要使一些简单的类型(如int和double)共享内存,请使用LayoutKind struct layout属性强制它们共享内存,然后将列表设置为该结构类型.例如:
[StructLayout(LayoutKind.Explicit)]
[CLSCompliant(false)]
public struct NumberStackEntry
{
/// <summary>Type of entry</summary>
[FieldOffset(0)]
public EntryTypes entryType;
/// <summary>Value if double</summary>
[FieldOffset(4)]
public double dval;
/// <summary>Value if ulong</summary>
[FieldOffset(4)]
public ulong uval;
/// <summary>Value if long</summary>
[FieldOffset(4)]
public long lval;
/// <summary>Value if integer</summary>
[FieldOffset(4)]
public int ival;
}
Run Code Online (Sandbox Code Playgroud)
编辑:您可以创建上面的数组,而无需装箱以使用这些值填充数组成员.但是,您无法将数组添加到结构中,并使其与非CLI引用类型共享内存.