gpt4 book ai didi

java - 如何加速在 Java 中进行字符串操作的循环?

转载 作者:行者123 更新时间:2023-11-29 09:39:20 34 4
gpt4 key购买 nike

<分区>

我有一个在循环中构建字符串的程序,但我的程序太慢了。现在运行 Oblig1Test.oppgave7 大约需要 600 毫秒。可以做些什么来加快速度?

Oblig1.toString:

public static String toString(int[] a, char v, char h, String mellomrom)
{
String s ="";

s += v;

if(a.length != 0)
{
for(int i = 0; i < a.length-1; i++)
{
s += a[i] + mellomrom;
}

s += a[a.length-1];
}

s += h;

return s;
}

Oblig1Test:

public static int oppgave7()
{
int[] b = new int[20000];
long tid = System.currentTimeMillis();
Oblig1.toString(b,' ',' '," ");
tid = System.currentTimeMillis() - tid;

if (tid > 40)
{
System.out.println("Oppgave 7: Metoden "
+ "er for ineffektiv. Må forbedres!");
}
}

public static void main(String[] args) throws IOException
{
oppgave7();
}

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com