提高java代码的效率
我编写了一些java代码来自动生成要导入MongoDB的JSON文件。 这是我的代码:提高java代码的效率,java,json,Java,Json,我编写了一些java代码来自动生成要导入MongoDB的JSON文件。 这是我的代码: import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; import java.util.Random; import org.apache.commons.lang3.RandomStringUtils; public class Coach
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
import org.apache.commons.lang3.RandomStringUtils;
public class Coach {
public static void main(String[] args) throws IOException
{
File f = new File("coach.json");
if(f.exists())
{
FileOutputStream fos = new FileOutputStream("coach.json", true);
PrintWriter pw = new PrintWriter(fos);
pw.append(createString());
System.out.println("The file \"Progettino.json\" already exists. New documents have been added at the bottom.");
pw.flush();
pw.close();
}
if(f.createNewFile())
{
PrintWriter pw = new PrintWriter(f);
pw.println(createString());
System.out.println("The file \"Progettino.json\" was created. New documents added.");
pw.flush();
pw.close();
}
}
public static String createString()
{
Random random = new Random();
String id = "{\n\"_id\":" + "\"";
String singleDocument="";
for(int i=0; i<10001;i++)
{
String name = "\"name\":" + "\"" + RandomStringUtils.randomAlphabetic(10) + "\",\n" ;
String surname = "\"surname\":" + "\"" + RandomStringUtils.randomAlphabetic(10) + "\",\n";
String age = "\"age\":" + "\"" + String.valueOf(random.nextInt(60)) + "\",\n ";
String date_of_birth = "\"date_Of_birth\":" + "{\"day\":" + String.valueOf(random.nextInt(31)) + "," +
"\"month\":" + String.valueOf(random.nextInt(12)) + ", " +
"\"year\":" + String.valueOf(random.nextInt(1900)) + "},\n";
String place_Of_birth = "\"place_Of_birth\":" + "\"" + RandomStringUtils.randomAlphabetic(10) + "\", \n";
String nationality = "\"nationality\":" + "\"" + RandomStringUtils.randomAlphabetic(10) + "\", \n";
String preferred_formation = "\"preferred_formation\":" + "\"" + RandomStringUtils.randomAlphabetic(10) + "\", \n";
String coached_Team = "\"coached_Team\": [" + "{\"team_id\":\"" + RandomStringUtils.randomAlphabetic(3) + "." + RandomStringUtils.randomAlphabetic(3) + "\"," +
"\"in_charge\": { \"from\":{" + "\"day\":" + String.valueOf(random.nextInt(31)) + "," +
"\"month\":" + String.valueOf(random.nextInt(12)) + ", " +
"\"year\":" + String.valueOf(random.nextInt(1900)) + "} }," +
"\"matches\":" + String.valueOf(random.nextInt(400)) + "},\n" +
"{\"team_id\":\"" + RandomStringUtils.randomAlphabetic(3) + "." + RandomStringUtils.randomAlphabetic(3) + "\"," +
"\"in_charge\": { \"from\":{" + "\"day\":" + String.valueOf(random.nextInt(31)) + "," +
"\"month\":" + String.valueOf(random.nextInt(12)) + ", " +
"\"year\":" + String.valueOf(random.nextInt(1900)) + "} }," +
"\"matches\":" + String.valueOf(random.nextInt(400)) + "}" +
"]\n}";
singleDocument = singleDocument + id + String.valueOf(i) + "\", \n" + name + surname + age + date_of_birth +
place_Of_birth + nationality + preferred_formation + coached_Team;
}
return singleDocument;
}
}
我的问题是这个程序效率不高。例如,如果我想生成10万个文档,大约需要30分钟。是否有可能使该代码更有效 您正在以最慢的方式创建一个非常长的字符串:重复复制和连接
我可以告诉您使用
StringBuilder
累积长字符串,但是完全跳过创建字符串会获得更好的性能。将PrintWriter
作为参数传递并直接写入 在我看来,这个问题更适合你。不管怎样,你为什么不使用现成的解决方案,比如Gson?对于您的代码,我想不起-使用StringBuilder
,而不是连接数千个不可变的String
s。非常感谢。我将PrintWriter作为参数传递,代码“仅”5秒钟就生成了100万个文档。
{
"_id":"30",
"name":"EDSDoIDENx",
"surname":"ygbECUwPap",
"age":"37",
"date_Of_birth":{"day":6,"month":4, "year":827},
"place_Of_birth":"HkGbFBaDIN",
"nationality":"yeEFVNmuvI",
"preferred_formation":"ITyvWidryK",
"coached_Team": [{"team_id":"ceV.iKu","in_charge": { "from":{"day":25,"month":7, "year":958} },"matches":103},
{"team_id":"gQQ.fdq","in_charge": { "from":{"day":15,"month":9, "year":121} },"matches":387}]
}
{
"_id":"31",
"name":"YoRYcdTjzk",
"surname":"rbpyGHqLjY",
"age":"43",
"date_Of_birth":{"day":17,"month":2, "year":524},
"place_Of_birth":"hWqooTGCHa",
"nationality":"eZDKzMelkt",
"preferred_formation":"vvrAhlJqji",
"coached_Team": [{"team_id":"TUI.HaY","in_charge": { "from":{"day":6,"month":0, "year":6} },"matches":182},
{"team_id":"fMh.IBi","in_charge": { "from":{"day":30,"month":7, "year":747} },"matches":204}]
}
{
"_id":"32",
"name":"DTAeGtWmrr",
"surname":"lxtYGXmgCh",
"age":"1",
"date_Of_birth":{"day":2,"month":6, "year":1208},
"place_Of_birth":"MtVqulnaQv",
"nationality":"iMrlbIpuCb",
"preferred_formation":"qkpcFdjXOz",
"coached_Team": [{"team_id":"FVo.ZWu","in_charge": { "from":{"day":14,"month":0, "year":937} },"matches":316},
{"team_id":"yaX.iZH","in_charge": { "from":{"day":22,"month":4, "year":1620} },"matches":275}]
}