wood burning stoves
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

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "Record count" Watch "Record count" New topic

Record count

saran sankar

Joined: Jan 16, 2002
Posts: 7
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(new Date());
System.out.println(new Date());
BufferedReader bf = new BufferedReader( new FileReader("test.txt"));
int count = 0;
while(bf.readLine() != null)
System.out.println(new Date());
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

Joined: Jan 16, 2002
Posts: 7
Thank you very much michal.
I agree. Here's the link: http://aspose.com/file-tools
subject: Record count
It's not a secret anymore!