Hallo,
ich habe mal etwas herumgespielt, da das reine Auslesen von 50 Mio Zeilen (was ca. 1.3 GB waren) ca. 10 Sekunden gedauert hat, vermute ich das Parsen ist das was dir die Performance zunichte macht.
Ich habe mal beides versucht, paralleles Lesen war zumindest mit meinem Ansatz nicht erfolgsversprechend weil scheinbar das „skippen“ der nicht gelesenen Zeilen ähnlich lange dauert wie das lesen selbst.
Das parallele Parsen zeigt jedoch deutliche Performanceverbesserung schon bei relativ wenig Zeilen (der Output wurde mit 5000 erstellt), wenn das Parsen eine gewisse Zeit in Anspruch nimmt (im Beispiel ein Sleep mit 2ms):
D:\temp\>java WWWTest parallelParsing
Active Threads: 16
finished in 1085 ms
D:\temp\>java WWWTest
Active Threads: 4
Reader\_2: 1620 lines read in 3268 ms
Active Threads: 3
Reader\_0: 1792 lines read in 3612 ms
Active Threads: 2
Reader\_1: 3286 lines read in 6634 ms
Active Threads: 1
finished in 7025 ms
Ich hoffe mein Quick ‚n‘ Dirty Beispiel lässt sich auf deinen Anwendungsfall übertragen.
Gruß
Heavy
import java.io.\*;
public class WWWTest
{
private static final File bigFile = new File("bigFile.txt");
// private static final int max = 50000000;
private static final int max = 5000;
public static void main(String[] args) throws IOException
{
if(args.length \> 0 && args[0].equals("createFile"))
createFile();
else if(args.length \> 0 && args[0].equals("parallelParsing"))
parallelParsing();
else
readFile();
}
private static class MyReader extends BufferedReader
{
private long count = 0;
private long max;
public MyReader(Reader reader, long skip, long max) throws IOException
{
super(reader);
skip(skip);
this.max = max;
}
@Override
public String readLine() throws IOException
{
String line = super.readLine();
if(line != null)
count += line.length();
return count 1)
{
try
{
Thread.sleep(500);
System.out.print("\rActive Threads: " + Thread.activeCount());
}
catch(InterruptedException e) {}
}
System.out.println("\n finished in " + (System.currentTimeMillis() - start) + " ms");
}
private static void readFile() throws IOException
{
long start = System.currentTimeMillis();
long skip = 23 \* (max / 3);
BufferedReader reader1 = new MyReader(new FileReader(bigFile), 0, skip);
BufferedReader reader2 = new MyReader(new FileReader(bigFile), skip, (2\*skip));
BufferedReader reader3 = new MyReader(new FileReader(bigFile), (2\*skip), Long.MAX\_VALUE);
BufferedReader[] readers = {reader1, reader2, reader3};
int tCount = 0;
for(final BufferedReader reader : readers)
{
Thread thread = new Thread("Reader\_" + (tCount++)) {
public void run()
{
try
{
long tStart = System.currentTimeMillis();
MyParser parser = new MyParser();
int count = 0;
String line = null;
while((line = reader.readLine()) != null)
{
count++;
parser.parseLine(line);
}
System.out.println("\n" + getName() + ": " + count + " lines read in " + (System.currentTimeMillis() - tStart) + " ms");
}
catch(IOException e)
{
System.err.println(e.getMessage());
}
}
};
thread.start();
}
while(Thread.activeCount() \> 1)
{
try
{
Thread.sleep(500);
System.out.print("\rActive Threads: " + Thread.activeCount());
}
catch(InterruptedException e) {}
}
System.out.println("\n finished in " + (System.currentTimeMillis() - start) + " ms");
}
private static void createFile() throws IOException
{
FileWriter writer = new FileWriter(bigFile);
for(int i=0; i