Question:
How to reduce execution time of java program?
Raksani
2012-04-12 10:10:24 UTC
I have developed a program to count the number of occurrence of a word and calculated the execution time of it. It seems to take 50sec for 187mb text file. How to reduce the execution time so that the program works faster. Help me please...!
// Here's the code
import java.io.*;
import java.util.*;

public class count
{
public static void main(String[] file) throws FileNotFoundException, IOException
{
String filename = "textfilename";
String inputLine = null;
long startTime = System.currentTimeMillis();
long endTime;
BufferedReader bufferedReader = null;
bufferedReader = new BufferedReader(new FileReader(filename));
TreeMap map = new TreeMap();

try
{
while ((inputLine = bufferedReader.readLine()) != null)
{
String[] words = inputLine.split("[\n\t\r., ;:!?(){}]");

for (String word : words)
{
Integer frequency = map.get(word);
if(t.contains(word))
{
frequency=0;
continue;
}
else

if (frequency == null)
{
frequency = 0;
}
frequency++;
map.put(word, frequency);
}
}

System.out.println(map);
endTime = System.currentTimeMillis();
System.out.println("Time taken :"+ (endTime-startTime)+" milliseconds");
}
}
}
Four answers:
?
2012-04-12 22:47:16 UTC
I think your execution time is perfect. because 187 MB text file is really large amount of Data. For long data it taken long time. Where is catch block???
?
2016-11-29 15:33:37 UTC
remember that the output habitual is likewise in a various thread so what you notice on the demonstrate screen is from a thread. I see no longer something incorrect with what's output. the main efficient thread runs somewhat, the CountDownEven thread runs somewhat and the housework runs somewhat.
James Bond
2012-04-12 10:20:55 UTC
May be you check for clock() function.

I feel 50sec is too much time
green meklar
2012-04-12 20:24:03 UTC
This code doesn't look complete. Where is t declared?


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...