-1

I have a code like this:

  RandomAccessFile raf = new RandomAccessFile(new File("C:\\Users\\AhmadMaleki\\IdeaProjects\\Hello world\\src\\kish\\file1"), "r");

for (int i = 1 ; i < 1025 ; i++)
        {
            for (int j = 0; j < raf.length() - 4 ; j++)
            {
                raf.seek(j);
                byte[] b = new byte[5];
                raf.read(b);

                if (new String (b).equals(wordcount[i]))
                {
                    z[i]++;
                }
            }
        }

In this Code raf.length() = 26841039 and execution time is 4100 minutes. Are there solutions to reduce run time?(for example multi thread, parallel, ....)

Ahmadit
  • 13
  • 1
  • 6
  • 2
    Welcome to Stack Overflow. This site is actually supposed to be for code which does not work. You want [Code Review](https://codereview.stackexchange.com/). – Michael Apr 20 '17 at 15:02
  • It's very likely that can be optimized. As an example, I really doubt you need to read the file 1024 times. Instead you might want to read it in one sweep, build some structure to hold the data and operate on that. – Thomas Apr 20 '17 at 15:05
  • Continuing I doubt you need RandomAccessFile for this. Reading continuously as a stream is faster. – Caspar Noree Apr 20 '17 at 15:08
  • 4
    Did you really wait almost 3 days for this to finish? – Michael Apr 20 '17 at 15:08
  • As an additional side note: be careful with `new String (b)`. This will use your system's default encoding and if that ever changes the resulting strings won't be what you're expecting. Better use `new String(b, "ISO-8859-1")`( I assume your bytes represent a character each so it's probably ASCII) – Thomas Apr 20 '17 at 15:14
  • You could use nio and map the file to memory, then divide the file in chunks and let a thread work on each chunk. But you need to have that much memory – bichito Apr 20 '17 at 15:23
  • 1
    Also i would put all of those words from `wordcount` into a `HashSet` so you can just check `if (words.contains(new String(b)) z[i]++` to eliminate the loop – paranoidAndroid Apr 20 '17 at 15:26

1 Answers1

1

For a start switch your loops so that you don't read the same file 1024 times.

for (int j = 0; j < raf.length() - 4 ; j++){
    for (int i = 1 ; i < 1025 ; i++){
      // Do your things here ... 
    }
}

Then choose your input stream wisely .

minus
  • 2,646
  • 15
  • 18
  • No need for this loop at all... just need to do what I wrote in the comment: put the words in a set and check it like this: `if (words.contains(new String(b)) z[i]++` – paranoidAndroid Apr 20 '17 at 15:30
  • There is no `i` if you remove the inner loop. By using a `Map` (instead of a set) you could in fact do : `map.set(word,map.get(word) + 1)`. It would be really an improvement. but maybe not so noticeable. – minus Apr 20 '17 at 17:02
  • Why do you need `i`? He just wants to check if the word is contained in the dictionary, so i think the `HashSet` should be enough, no? the whole point of the loop as i get it is just to check all the words in the dictionary – paranoidAndroid Apr 21 '17 at 17:00
  • Whoops, you're right I've missed that part :) so yeah using the `Map` as you suggested is the solution – paranoidAndroid Apr 22 '17 at 10:01