tags:

views:

171

answers:

2

I'm doing what seems like a simple read from a data file, and it's taking for fricking ever. (And by that, I mean 300ms.) This is the code in question:

BufferedReader f = new BufferedReader(new FileReader("input.txt"));
StringTokenizer st = new StringTokenizer(f.readLine());
int var1 = Integer.parseInt(st.nextToken())
int var2 = Integer.parseInt(st.nextToken());
Integer[][] people = new Integer[var1][];
for(int i = 0; i < var2; i++)
    f.readLine();
for(Integer i = 0; i < var1; i++)
{
    StringTokenizer line = new StringTokenizer(f.readLine(), " \t\n\r\f,");
    line.nextToken();
    Integer[] list = new Integer[line.countTokens()];
    for(int j = 0; j < drinks.length; j++)
        list[j] = Integer.parseInt(line.nextToken());
    people[i] = list;
}

And this is the relevant time output:

sammysmbp:fridgemadness_lean sfishman$ time java fridgemadness

real    0m0.311s
user    0m0.277s
sys  0m0.056s

Is it just me or is that really, really slow. It's only going through 51 lines of input in this example.

+3  A: 

Just guessing, but probably a lot of the time is used up just for the jvm to start up.

Print a timestamp just before and after the relevant code. If my guess is correct a little server taking requests might help to eliminate the startup time.

Jens Schauder
Thanks. Just timed it within the program itself, and it completes in 1ms. All the other time is spent in the system apparently.
deftonix
+1  A: 

You don't need \r or \n in the pattern, readLine() has already removed them.

JVM startup aside, most of the time will be spent in reading to the desired position if the file is at all long. Maybe a text file of lines isn't the correct data structure.

EJP