我发现这个问题只有少数威胁,但似乎没有解决它。
我的代码在Eclipse中运行10秒钟。如果我将此项目提取到可运行的jar文件中,则需要5-10分钟。
我的所作所为:我读了两个.txt文件,将它们合并,排序,分开并保存。 这是我的代码:
package de.***.timcooparser;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import com.google.code.externalsorting.ExternalSort;
public class TimCooParser {
private static List<String> listCoordinates = new ArrayList<String>();
private static List<String> listTimestamps = new ArrayList<String>();
private static List<String> listTimeCoord = new ArrayList<String>();
public static void main(String[ ] args) throws IOException {
//Start Time
long d1 = System.currentTimeMillis();
//Create Path
String path = new File("").getAbsolutePath();
//Readfiles
File fileTimeCoord = new File (path+"\\TimestampsCoordinates.txt");
File fileTimeCoord2 = new File (path+"\\TimestampsCoordinates2.txt");
File fileCoordinates = new File (path+"\\Coordinates.txt");
File fileTimestamps = new File (path+"\\Timestamps.txt");
try{
if (fileTimeCoord.exists()){
fileTimeCoord.delete();
}
fileCoordinates.createNewFile();
//Read Coordinates an Timestamps and merge them
InputStream is1 = new FileInputStream(fileTimestamps);
InputStreamReader instrm1 = new InputStreamReader(is1);
BufferedReader br1 = new BufferedReader(instrm1);
InputStream is2 = new FileInputStream(fileCoordinates);
InputStreamReader instrm2 = new InputStreamReader(is2);
BufferedReader br2 = new BufferedReader(instrm2);
String line = "", line2 = "";
Integer i = 0;
while ((line = br1.readLine()) != null){
line2 = br2.readLine();
String[] arrayTemp = line2.split(" ");
listTimeCoord.add(line+","+arrayTemp[0] + "," + arrayTemp[1]);
i++;
if (i>=500){
i=0;
saveFiles(fileTimeCoord);
}
}
br1.close();
br2.close();
//Sort File and delete old file
ExternalSort.sort(fileTimeCoord, fileTimeCoord2);
fileTimeCoord.delete();
//Delete old files
if (fileCoordinates.exists()){
fileCoordinates.delete();
}
fileCoordinates.createNewFile();
if (fileTimestamps.exists()){
fileTimestamps.delete();
}
fileTimestamps.createNewFile();
//Read TimeCoord and override Timestamps and Coordinates
InputStream is3 = new FileInputStream(fileTimeCoord2);
InputStreamReader instrm3 = new InputStreamReader(is3);
BufferedReader br3 = new BufferedReader(instrm3);
line = "";
listCoordinates.clear();
listTimestamps.clear();
Integer ct = 0;
while ((line = br3.readLine()) != null){
String[] arrayTemp = line.split(",");
listTimestamps.add(arrayTemp[0]);
listCoordinates.add(arrayTemp[1] + " " + arrayTemp[2]);
ct++;
if (ct >= 500){
saveFiles(fileTimestamps, fileCoordinates);
}
}
br3.close();
// Delete old Files
fileTimeCoord2.delete();
//get end Time
long d2 = System.currentTimeMillis();
//Get diff time
long diff = d2 - d1;
String diffTime = "Finished in " + ((diff / (60 * 60 * 1000)) % 24) + " hours " + ((diff / (60 * 1000)) % 60) + " minutes and "
+ ((diff / 1000) % 60) + " seconds";
JOptionPane.showMessageDialog(null, "Parser finished in: \n" + diffTime,"TimesCoord Parser", JOptionPane.INFORMATION_MESSAGE);
}
catch (FileNotFoundException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null,"Parser Error:\n" + e,"TimesCoord Parser", JOptionPane.CANCEL_OPTION);
}
catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null,"Parser Error:\n" + e,"TimesCoord Parser", JOptionPane.CANCEL_OPTION);
}
}
private static void saveFiles(File fileTimestamps, File fileCoordinates) throws IOException {
List<String> listCoordinates2 = new ArrayList<String>();
List<String> listTimestamps2 = new ArrayList<String>();
listCoordinates2.addAll(listCoordinates);
listCoordinates.clear();
listTimestamps2.addAll(listTimestamps);
listTimestamps.clear();
//Write to File
BufferedWriter bw1 = new BufferedWriter( new FileWriter(fileTimestamps, true));
for (String string : listTimestamps2) {
bw1.append(string + "\n");
}
bw1.close();
//Write to File
BufferedWriter bw2 = new BufferedWriter( new FileWriter(fileCoordinates, true));
for (String string : listCoordinates2) {
bw2.append(string + "\n");
}
bw2.close();
}
private static void saveFiles(File fileTimeCoord) throws IOException {
List<String> listTimeCoord2 = new ArrayList<String>();
listTimeCoord2.addAll(listTimeCoord);
listTimeCoord.clear();
//Write to File
BufferedWriter bw1 = new BufferedWriter( new FileWriter(fileTimeCoord, true));
for (String string : listTimeCoord2) {
bw1.append(string + "\n");
}
bw1.close();
}
public static String TimestampToDate(long stringDate)
{
DateTimeZone zone = DateTimeZone.forID("Europe/Berlin");
DateTimeZone.setDefault(zone);
DateTime actual = new DateTime(stringDate);
String dateString = actual.toString("yyyy-MM-dd'T'HH:mm:ssZZ");
return dateString;
}
}
eclipse和我的PC确实使用相同的JVM:
java version "1.8.0_20"
Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
Java HotSpot(TM) Client VM (build 25.20-b23, mixed mode, sharing)
这是我的eclipse.ini:
-startup
plugins/org.eclipse.equinox.launcher_1.3.0.v20140415-2008.jar
--launcher.library
plugins/org.eclipse.equinox.launcher.win32.win32.x86_1.1.200.v20140603-1326
-product
org.eclipse.epp.package.java.product
--launcher.defaultAction
openFile
--launcher.XXMaxPermSize
256M
-showsplash
org.eclipse.platform
--launcher.XXMaxPermSize
256m
--launcher.defaultAction
openFile
--launcher.appendVmargs
-vmargs
-Dosgi.requiredJavaVersion=1.6
-Xms512m
-Xmx1024m
我该怎么做才能解决这个问题?
PS:它独立于&#34; externalsortinginjava ..&#34;库。
答案 0 :(得分:0)
尝试确保使用Eclipse在启动应用程序时所执行的相同内存选项。
看起来命令行参数应为-Xms512m -Xmx1024m
答案 1 :(得分:0)
我找到了需要很多时间的线路。
if (ct >= 500){
saveFiles(fileTimestamps, fileCoordinates);
}
我将此更改为
if (ct >= 1000000000){
saveFiles(fileTimestamps, fileCoordinates);
ct=0;
}
}
br3.close();
saveFiles(fileTimestamps, fileCoordinates);
上面的变化(i> 500的代码)。
现在它需要1秒以内的Eclipse,以及jar文件3秒。
但是为什么Eclipse中的代码和jar文件一样快,但是没有解决!