I have found only a small number of Threats about this Problem, but nothing seems to solve it.
My code runs in Eclipse under 10 seconds. If I extract this Project into a runnable jar file it takes over 5-10 minutes.
What I do: I read two .txt files, combine them, sort them, separate them and save them. This is my code:
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;
}
}
The eclipse and my PC do use the same 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)
And here is my 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
What can I do to solve this Problem?
PS: It is independent of the "externalsortinginjava.." library.
Try making sure that you use the same memory options that Eclipse does when launching the app.
Looks like the command line arguments should be -Xms512m -Xmx1024m
I have found the line which does need a lot of time.
if (ct >= 500){
saveFiles(fileTimestamps, fileCoordinates);
}
I changed this to
if (ct >= 1000000000){
saveFiles(fileTimestamps, fileCoordinates);
ct=0;
}
}
br3.close();
saveFiles(fileTimestamps, fileCoordinates);
And the same above the changes (the code with i>500).
Now it takes in Eclipse under 1 Second and as jar File 3 Seconds.
But why the code is faster in Eclipse as as jar file, is not solved!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.