C#中:为INT []转换为字符串的最有效方法 [英] C#: the most efficient way to convert int[] into a string

查看:155
本文介绍了C#中:为INT []转换为字符串的最有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这种问题已经回答了很多次。虽然我发现很多可能的答案,他们仍然不解决我的问题,这是以实现最快的方式为整数数组转换成一个字符串。我有一个例子:

I do know that this kind of questions have already been answered many times. Although I've found lots of possible answers, they still don't solve my problem, which is to implement the fastest possible way to convert an integer array into a single string. I have for example:

int[] Result = new int[] { 1753387599, 1353678530, 987001 }

我想它颠倒过来,所以我相信这是最好的precede进一步code以

I want it reversed, so I believe it's best to precede the further code with

Array.Reverse(Result);

虽然我不从最终迭代,这相当于逆转,因为我把从最终的元素。所以,我已经做到了这一点。只是为了让你知道 - 如果你能想到比我的任何其他解决方案,我建议使用这种Array.Reverse,因为该解决方案必须扭转。
我总是只关心一个号码的最后9位数字 - 所以像模1 000 000 000这里是我想要得到什么:

Although I don’t iterate from the end, it’s equivalent to reversing, because I call elements from the end. So I have already done this. Just to let you know - if you can think of any other solution than mine, I suggest using this Array.Reverse, because the solution must be reversed. I always care only about the last 9 digits of a number - so like modulo 1 000 000 000. Here is what I'd like to get:

987001|353678530|753387599

分离器只是把它现在很清楚。我写我自己的功能比使用的ToString()快约50%。
tempint - 在int数组的当前元素,
StrArray - 一个字符串数组。这是不值得使用StringBuilder的总结或
字符串,所以在最后我干脆加入AnswerArr的元素来获取结果。
IntBase - 包含1000个元素,数字字符串中从000到999的数组,索引0〜999

Separators just to have it clear now. I wrote my own function that is about 50% faster than using .ToString(). tempint - current element of the int array, StrArray - a string array. It's not worth using StringBuilder or summing strings, so at the end I simply join the elements of the AnswerArr to get the result. IntBase - an array containing 1000 elements, numbers in strings from "000" to "999", indexed 0 to 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毫秒。

There ale lots of calculations before this part and they're are done very fast. While everything goes in 714 ms, without summing integers, it's just 337 ms.

感谢您事先的任何帮助。

Thanks in advance for any help.

最好的问候,
兰多夫

Best regards, Randolph

推荐答案

更快?大多数efficent?我不知道,你应该尝试一下。但是,一个简单的方法来转换

Faster? Most efficent? I am not sure, you should try it. But a simple way to convert

int[] Result = new int[] { 1753387599, 1353678530, 987001 };
var newstr = String.Join("|", Result.Reverse().Select(i => i % 1000000000));

这篇关于C#中:为INT []转换为字符串的最有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆