小编Sam*_*esh的帖子

字符串连接时的性能 - 算法字符串字符串c#

我使用以下代码追加字符串

string res = string.Empty;
int ite = 100000;
for(int i= 0; i < ite; i++)
{
    res += "5";
}
Run Code Online (Sandbox Code Playgroud)

这花费了很多时间,所以我后来将代码更改为

string res = string.Empty;
int ite = 100000;
res = getStr(ite / 2) + getStr(ite - (ite / 2)); 

//body of getStr method
private static string getStr(int p)
{
    if (p == 1)
        return "5";
    else if (p == 0)
        return string.Empty;
    string r1 = getStr(p / 2); //recursive
    string r2 = getStr(p - (p / 2)); //recursive …
Run Code Online (Sandbox Code Playgroud)

c# string algorithm string-concatenation

4
推荐指数
1
解决办法
812
查看次数

标签 统计

algorithm ×1

c# ×1

string ×1

string-concatenation ×1