#939 class run from interactions can be very slow

4: Serious
open
nobody
5
2013-03-31
2013-03-31
Anonymous
No

Using algs4.jar from http://algs4.cs.princeton.edu/code/

this code is slow (1000 secs for reading a 8 MB file, runtime grows log2arithmic with file size):
In synfile = new In(filename);
Bag<String> synlines = new Bag<String>();
while (synfile.hasNextLine())
synlines.add(synfile.readLine());

but this is fast (1-2 sec):
In synfile = new In(filename);
ResizingArrayStack<String> synlines = new ResizingArrayStack<String>();
while (synfile.hasNextLine())
synlines.push(synfile.readLine());

However both of the codes are fast when run from opsystem command-line.
The main difference is Bag uses a private Node class and creates a new one in each add() operation, ResizingArray doesn't.

Discussion


Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks