0

I'm writing a program which reads a lot of data files and finds special points in them. For that, I'm using the Scanner, which can parse the data (Which looks like that:)

-0.46368701 0.02136296

-0.46304701 0.03045747

-0.46240701 0.03045747

The first double goes to one array and the second to another.

For the question: the program is too slow. Can I use bufferedReader well in this case? I don't know if parsing it's argument will make the program insufficient.

Thank you!

BTW - the main methods of the program look like that:

    private void init() throws IOException{     
    try {
        scan = new Scanner(file);
    } catch (FileNotFoundException e) {
        System.out.println(e.toString());
        e.printStackTrace();
    }       
    lines = countLines(file);
    value = new double[lines];
    time = new double[lines];

    vMinValue = new Vector<Double>();
    vMinTime = new Vector<Double>();
}

//fill in time[] and value[] arrays
private void readFile(){

   count=0;

   System.out.println("Reading file...\n");

     //Get to the start of the info -=CORE=-
     while(scan.hasNext()){
         if(scan.next().equals("(V)")){
             System.out.println("Reading only 'Channel A' Values \n ");              
             break;
         }
     }   

     while(scan.hasNext()){

        time[count] = scan.nextDouble();            
        value[count] = scan.nextDouble();
        count++;

        if(value[count]==Double.NEGATIVE_INFINITY) count--;

     }           
     scan.close();
     System.out.println("Input File Has Been Closed \n");


  }

    private void addMins(double[] a,int range){
    for(int i=0;i<a.length;i++){
        if( isMin(i,range)){ 
            if((vMinValue.isEmpty() || vMinValue.get(vMinValue.size()-1)!=a[i])){ // provides only one minimum point
                vMinValue.add((Double)value[i]);
                vMinTime.add((Double)time[i]);
                managMin.add(value[i]);
            }
        }
    }

    private boolean isMin(int i,int range){
    boolean result = true;

    for(int j=-range;j<=range;j++){
        if(j==0) continue;
        if(i+j<0 || i+j>value.length-1) continue;
        if(value[i]>value[i+j]) return false;
    }


    return result;
}
YosefBro
  • 596
  • 1
  • 7
  • 17

1 Answers1

0

Read this: Scanner vs. BufferedReader

All in all, bufferedreader is faster but that's because it doesn't tokenize. Where as Scanner does. Now if you want to use bufferedreader you can you just need to sort the data yourself but that takes more code and it won't be necessarily faster.

When faced with these problems always try yourself. Why not make one that uses bufferedreader yourself? It will be an experience.

Now if you feel like your program is ridiculously slow... like obviously something wrong with it type of slow then you need to show more code and do more debugging

Community
  • 1
  • 1
Ya Wang
  • 1,758
  • 1
  • 19
  • 41
  • It's not so slow, but i think it could be faster. Now, when i get my line by bufferedreader, i need to make manipulations on it, and i don't know a quick way for this either than scanner (a new scanner for each new line does not sound so good).. – YosefBro May 04 '15 at 18:52
  • Well once you read a line in bufferedreader you can use string manipulation ex... string.split(" ") and get 2 String or you can use StringTokenizer https://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html which is what Scanner uses – Ya Wang May 04 '15 at 18:57
  • StringTokenizer seems to be the right solution here. Can you advice me how should I implement it? – YosefBro May 04 '15 at 20:16