Commit 0dc448df authored by Stefan Schuhbaeck's avatar Stefan Schuhbaeck
Browse files

Use BufferedReader instead of ReadAllLines.

It seems to be more efficient in a small test.
Time measured in nanoseconds in a loop (3x).

(postvis.trajectories)  readTextFile:   139179300
(postvis.trajectories)  readTextFile2:   52790478 <-- new
(test_postvis.scenario) readTextFile3:    4355102
(test_postvis.scenario) readTextFile4:    1037918 <-- new
---------------------------------------------------------
(postvis.trajectories)  readTextFile:    37730947
(postvis.trajectories)  readTextFile2:    7910077 <-- new
(test_postvis.scenario) readTextFile3:    6125829
(test_postvis.scenario) readTextFile4:     484034 <-- new
---------------------------------------------------------
(postvis.trajectories)  readTextFile:    22605907
(postvis.trajectories)  readTextFile2:    6541635 <-- new
(test_postvis.scenario) readTextFile3:     688422
(test_postvis.scenario) readTextFile4:     415274 <-- new
---------------------------------------------------------
parent ef49cad8
......@@ -10,11 +10,17 @@ import org.vadere.state.scenario.Agent;
import org.vadere.state.scenario.Pedestrian;
import org.vadere.state.simulation.Step;
import org.vadere.util.geometry.shapes.VPoint;
import org.vadere.util.io.IOUtils;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.stream.Collectors;
/**
......@@ -22,14 +28,10 @@ import java.util.stream.Collectors;
*/
public class TrajectoryReader {
private static final String SPLITTER = " ";
private static Logger logger = LogManager.getLogger(IOVadere.class);
private Path trajectoryFilePath;
private AttributesAgent attributesPedestrian;
private static final String SPLITTER = " ";
private Set<String> pedestrianIdKeys;
private Set<String> stepKeys;
private Set<String> xKeys;
......@@ -51,84 +53,84 @@ public class TrajectoryReader {
}
private TrajectoryReader(final Path trajectoryFilePath, final AttributesAgent attributesAgent) {
this.trajectoryFilePath = trajectoryFilePath;
this.attributesPedestrian = attributesAgent;
pedestrianIdKeys = new HashSet<>();
stepKeys = new HashSet<>();
xKeys = new HashSet<>();
yKeys = new HashSet<>();
targetIdKeys = new HashSet<>();
pedestrianIdKeys.add("id");
pedestrianIdKeys.add("pedestrianId");
stepKeys.add("timeStep");
stepKeys.add("step");
xKeys.add("x");
yKeys.add("y");
targetIdKeys.add("targetId");
pedIdIndex = -1;
stepIndex = -1;
xIndex = -1;
yIndex = -1;
targetIdIndex = -1;
}
this.trajectoryFilePath = trajectoryFilePath;
this.attributesPedestrian = attributesAgent;
pedestrianIdKeys = new HashSet<>();
stepKeys = new HashSet<>();
xKeys = new HashSet<>();
yKeys = new HashSet<>();
targetIdKeys = new HashSet<>();
pedestrianIdKeys.add("id");
pedestrianIdKeys.add("pedestrianId");
stepKeys.add("timeStep");
stepKeys.add("step");
xKeys.add("x");
yKeys.add("y");
targetIdKeys.add("targetId");
pedIdIndex = -1;
stepIndex = -1;
xIndex = -1;
yIndex = -1;
targetIdIndex = -1;
}
public Map<Step, List<Agent>> readFile() throws IOException {
// 1. Get the correct column
String header = Files.lines(this.trajectoryFilePath).findFirst().get();
String[] columns = header.split(SPLITTER);
for(int index = 0; index < columns.length; index++) {
if(pedestrianIdKeys.contains(columns[index])) {
pedIdIndex = index;
}
else if(stepKeys.contains(columns[index])) {
stepIndex = index;
}
else if(xKeys.contains(columns[index])) {
xIndex = index;
}
else if(yKeys.contains(columns[index])) {
yIndex = index;
}
else if(targetIdKeys.contains(columns[index])) {
targetIdIndex = index;
}
}
try {
if(pedIdIndex != -1 && xIndex != -1 && yIndex != -1 && stepIndex != -1) {
return Files.lines(this.trajectoryFilePath)
.skip(1) // Skip header line
.map(line -> line.split(SPLITTER))
.map(cells -> {
int step = Integer.parseInt(cells[stepIndex]);
int pedestrianId = Integer.parseInt(cells[pedIdIndex]);
VPoint pos = new VPoint(Double.parseDouble(cells[xIndex]), Double.parseDouble(cells[yIndex]));
int targetId = targetIdIndex != -1 ? Integer.parseInt(cells[targetIdIndex]) : -1;
Pedestrian ped = new Pedestrian(new AttributesAgent(this.attributesPedestrian, pedestrianId), new Random());
ped.setPosition(pos);
LinkedList<Integer> targets = new LinkedList<Integer>();
targets.addFirst(targetId);
ped.setTargets(targets);
return Pair.create(new Step(Integer.parseInt(cells[0])), ped);
})
.collect(Collectors.groupingBy(pair -> pair.getKey(), Collectors.mapping(pair -> pair.getValue(), Collectors.toList())));
}
else {
throw new IOException("could not read trajectory file, some colums are missing.");
}
}
catch (Exception e) {
logger.warn("could not read trajectory file. The file format might not be compatible or it is missing.");
throw e;
}
// 1. Get the correct column
String header;
//read only first line.
try (BufferedReader in = IOUtils.defaultBufferedReader(this.trajectoryFilePath)) {
header = in.readLine();
}
String[] columns = header.split(SPLITTER);
for (int index = 0; index < columns.length; index++) {
if (pedestrianIdKeys.contains(columns[index])) {
pedIdIndex = index;
} else if (stepKeys.contains(columns[index])) {
stepIndex = index;
} else if (xKeys.contains(columns[index])) {
xIndex = index;
} else if (yKeys.contains(columns[index])) {
yIndex = index;
} else if (targetIdKeys.contains(columns[index])) {
targetIdIndex = index;
}
}
try {
if (pedIdIndex != -1 && xIndex != -1 && yIndex != -1 && stepIndex != -1) {
try (BufferedReader in = IOUtils.defaultBufferedReader(this.trajectoryFilePath)) {
return in.lines()
.skip(1) //Skip header line
.map(line -> line.split(SPLITTER))
.map(cells -> {
int step = Integer.parseInt(cells[stepIndex]);
int pedestrianId = Integer.parseInt(cells[pedIdIndex]);
VPoint pos = new VPoint(Double.parseDouble(cells[xIndex]), Double.parseDouble(cells[yIndex]));
int targetId = targetIdIndex != -1 ? Integer.parseInt(cells[targetIdIndex]) : -1;
Pedestrian ped = new Pedestrian(new AttributesAgent(this.attributesPedestrian, pedestrianId), new Random());
ped.setPosition(pos);
LinkedList<Integer> targets = new LinkedList<Integer>();
targets.addFirst(targetId);
ped.setTargets(targets);
return Pair.create(new Step(Integer.parseInt(cells[0])), ped);
})
.collect(Collectors.groupingBy(pair -> pair.getKey(), Collectors.mapping(pair -> pair.getValue(), Collectors.toList())));
}
} else {
throw new IOException("could not read trajectory file, some colums are missing.");
}
} catch (Exception e) {
logger.warn("could not read trajectory file. The file format might not be compatible or it is missing.");
throw e;
}
}
}
package org.vadere.util.io;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
......@@ -13,10 +16,10 @@ import java.nio.file.Paths;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.StringJoiner;
import java.util.prefs.BackingStoreException;
import java.util.prefs.InvalidPreferencesFormatException;
import java.util.prefs.Preferences;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
......@@ -150,17 +153,20 @@ public class IOUtils {
}
}
public static BufferedReader defaultBufferedReader(Path filePath) throws FileNotFoundException {
return new BufferedReader(new InputStreamReader(
new FileInputStream(filePath.toFile()), StandardCharsets.UTF_8),8192*1);
}
/** Reads all text of a given file and store it in a string. */
public static String readTextFile(Path filePath) throws IOException {
List<String> lines = Files.readAllLines(filePath, StandardCharsets.UTF_8);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < lines.size(); i++) {
sb.append(lines.get(i));
if (i < lines.size() - 1) {
sb.append(System.lineSeparator());
}
public static String readTextFile(Path filePath) throws IOException{
StringJoiner sb = new StringJoiner(System.lineSeparator());
try(BufferedReader inputStream = IOUtils.defaultBufferedReader(filePath)){
String line;
while ((line = inputStream.readLine()) != null)
sb.add(line);
return sb.toString();
}
return sb.toString();
}
public static String readTextFile(String filePath) throws IOException {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment