This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Performance and the fly likes Record count Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Performance
Bookmark "Record count" Watch "Record count" New topic
Author

Record count

saran sankar
Greenhorn

Joined: Jan 16, 2002
Posts: 7
Hi,
I want to know is there any efficient way to find out the number of records in the variable length text file.
I have tried 2 ways using BufferedReader & LineNumberReader but I am getting a same performance.To count 50,000 records both APIs taking 12 seconds.But is there any other way to calculate the above functionality.
Code ex
-------
System.out.println(new Date());
LineNumberReader in = new LineNumberReader(new FileReader("test.txt"));
while(in.readLine() != null);
System.out.println(in.getLineNumber());
System.out.println(new Date());
in.close();
System.out.println(new Date());
BufferedReader bf = new BufferedReader( new FileReader("test.txt"));
int count = 0;
while(bf.readLine() != null)
count++;
System.out.println(count);
System.out.println(new Date());
Thanks
Saran.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
The key here is to first of all use a lower level InputStream and then let parallel processing do the work for you:

I got a record count of 50034 on my test.txt (which I manufactured from some long system log files) in about 2 seconds on a P2-400 (a real slowpoke). So you should be able to cut this down to about a second.
Hope this helps.


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
saran sankar
Greenhorn

Joined: Jan 16, 2002
Posts: 7
Thank you very much michal.
Saran.
 
GeeCON Prague 2014
 
subject: Record count