我确实知道此类问题已经被回答过很多次了。尽管我找到了很多可能的答案,但它们仍然没有解决我的问题,即实现将整数数组转换为单个字符串的最快方法。我有例如:
int[] Result = new int[] { 1753387599, 1353678530, 987001 }
我希望它颠倒过来,所以我相信最好在进一步的代码之前加上
Array.Reverse(Result);
虽然我不是从末尾开始迭代,但是相当于倒转,因为我是从末尾调用元素的。所以我已经这样做了。只是为了让您知道 - 如果您能想到除我之外的任何其他解决方案,我建议使用此 Array.Reverse,因为解决方案必须反转。
我总是只关心数字的最后 9 位 - 就像模 1 000 000 000 一样。这是我想要得到的:
987001|353678530|753387599
分隔符现在就清楚了。我编写了自己的函数,该函数比使用 .ToString() 快约 50%。
tempint - int 数组的当前元素,
StrArray - 字符串数组。不值得使用 StringBuilder 或求和
字符串,所以最后我只需连接 AnswerArr 的元素即可获得结果。
IntBase - 包含 1000 个元素的数组,数字为从“000”到“999”的字符串,索引为 0 到 999。
for (i = 0; i < limit; i++)
{
//Some code here
j = 3 * (limit - i);
//Done always
StrArray[j - 1] = IntBase[tempint % 1000];
if (tempint > 999999)
{
//Done in 99/100 cases
StrArray[j - 2] = IntBase[tempint % 1000000 / 1000];
StrArray[j - 3] = IntBase[tempint % 1000000000 / 1000000];
}
else
{
if (tempint > 999)
{
//Done just once
StrArray[j - 2] = IntBase[tempint % 1000 / 1000];
}
}
}
//Some code here
return string.Join(null, StrArray);
在这部分之前有很多计算,而且完成得非常快。虽然一切都在 714 毫秒内完成,但如果不将整数相加,则只有 337 毫秒。
预先感谢您的任何帮助。
此致,
伦道夫