C# 如何创建大型整数数组来测试LongCount?
我想分配一个大的整数数组来测试LongCount操作符。在以下情况下使用LongCount运算符: 您希望结果大于MaxValue 为了准备我的测试,我想分配一个整数数组,它只比C# 如何创建大型整数数组来测试LongCount?,c#,.net,arrays,C#,.net,Arrays,我想分配一个大的整数数组来测试LongCount操作符。在以下情况下使用LongCount运算符: 您希望结果大于MaxValue 为了准备我的测试,我想分配一个整数数组,它只比Int32.MaxValue稍大一点: Int64[] arr = new Int64[Int32.MaxValue + 10UL]; 但这会抛出一个溢出异常 我想做的是这样的: Int64[] arr = new Int64[Int32.MaxValue + 10UL]; var res = arr.LongCou
Int32.MaxValue
稍大一点:
Int64[] arr = new Int64[Int32.MaxValue + 10UL];
但这会抛出一个溢出异常
我想做的是这样的:
Int64[] arr = new Int64[Int32.MaxValue + 10UL];
var res = arr.LongCount();
然后期望res
为2147483657
(即Int32.MaxValue+10
)
我该怎么做呢?问题是一个数组可以容纳的最大大小是
System.Int32.MaxValue
,当你想创建一个数组,它的元素比System.Int32.MaxValue
多时,它会抛出一个溢出异常
你可以编写自己的列表实现来存储多个数组和将它们联系在一起(或者很可能在某些地方已经有了更好的方法)。映射巨大的ulong
inttwoInt32
索引,在其上实现IEnumerable
接口,然后进行测试
ulong listSize = Int32.MaxValue + 10UL;
BigList<bool> myList = new BigList<bool>(listSize);
Debug.Assert(myList.LongCount() == (long)listSize);
Console.ReadKey();
ulong listSize=Int32.MaxValue+10UL;
BigList myList=新的BigList(listSize);
Assert(myList.LongCount()==(long)listSize);
Console.ReadKey();
示例实现
public class BigList<T> : IEnumerable<T>
{
private List<T[]> _storage = new List<T[]>();
private const int _maxStorageArraySize = 1000;
public ulong Capacity { get; private set; }
public BigList(ulong capacity)
{
_storage = new List<T[]>();
Capacity = capacity;
int arraysRequired = (int)Math.Ceiling((double)capacity / (double)_maxStorageArraySize);
int lastArraySize = (int)(capacity % (ulong)_maxStorageArraySize);
for (int i = 0; i < arraysRequired; i++)
_storage.Add(new T[(i + 1) < arraysRequired ? _maxStorageArraySize : lastArraySize]);
}
public T this[ulong idx]
{
get
{
int arrayIdx = (int)(idx / (ulong)_maxStorageArraySize);
int arrayOff = (int)(idx % (ulong)_maxStorageArraySize);
return _storage[arrayIdx][arrayOff];
}
set
{
int arrayIdx = (int)(idx / (ulong)_maxStorageArraySize);
int arrayOff = (int)(idx % (ulong)_maxStorageArraySize);
_storage[arrayIdx][arrayOff] = value;
}
}
public class BigListEnumerator : IEnumerator<T>
{
private BigList<T> _bigList;
private ulong _idx;
public BigListEnumerator(BigList<T> bigList)
{
_bigList = bigList;
}
public T Current
{
get { return _bigList[_idx]; }
}
public void Dispose()
{
_bigList = null;
}
object System.Collections.IEnumerator.Current
{
get { return Current; }
}
public bool MoveNext()
{
return _idx++ < _bigList.Capacity;
}
public void Reset()
{
_idx = 0;
}
}
public IEnumerator<T> GetEnumerator()
{
return new BigListEnumerator(this);
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return new BigListEnumerator(this);
}
}
公共类BigList:IEnumerable
{
私有列表_存储=新列表();
私有常量int_maxStorageArraySize=1000;
公共ulong容量{get;private set;}
公共大名单(乌龙容量)
{
_存储=新列表();
容量=容量;
int arraysRequired=(int)数学上限((双)容量/(双)\最大存储阵列大小);
int lastArraySize=(int)(容量%(ulong)\u最大存储阵列大小);
for(int i=0;i
您不需要数组,也不应该使用数组。此测试不需要专用2GB内存(使用字节[]
)。仅此而已:
var items = Enumerable.Range(0, int.MaxValue).Concat(Enumerable.Range(0, 10));
用它作为来源。或者编写自己的
RangeLong
方法,该方法由一个for循环组成,该循环在一个长变量上进行测距。这将更快,因为项目通过的迭代器更少。在这么多的项目中,性能成为一个问题。您正在测试.NET framework吗?安装更多RAM,使用byte
而不是Int64
我投票结束这个问题,因为它不是建设性的,我仍然不明白测试LongCount的目的是什么??您到底想测试什么?您可以将它们与长度为10
的数组进行比较,其中一个返回int
,另一个返回long
CLR正在将您从自己手中解救出来;-)如果分配的内存不超过物理内存+交换分配内存,为什么需要数组来测试LongCount?它是IEnumerable的属性?不需要这样做。如果你想亲眼看到并相信某些东西是有效的,无论你想以什么方式,为什么不呢?旁注——对于这个问题,真的没有必要使用分块数组实现。收益率为1的基本计数器代码>应该是演示的全部内容…为什么需要在枚举器下面有实际的数组?带有long
index的for
循环将产生返回值
ing零,这样做很好。绝对是矫枉过正:)