public static void main(String[] args) throws IOException {
    WordCounter wordCounter = new WordCounter();
    Folder folder = Folder.fromDirectory(new File(args[0]));

    final int repeatCount = Integer.decode(args[2]);
    long counts;
    long startTime;
    long stopTime;

    long[] singleThreadTimes = new long[repeatCount];
    long[] forkedThreadTimes = new long[repeatCount];

    for (int i = 0; i < repeatCount; i++) {
      startTime = System.currentTimeMillis();
      counts = wordCounter.countOccurrencesOnSingleThread(folder, args[1]);
      stopTime = System.currentTimeMillis();
      singleThreadTimes[i] = (stopTime - startTime);
      System.out.println(counts + " , single thread search took " + singleThreadTimes[i] + "ms");
    }

    for (int i = 0; i < repeatCount; i++) {
      startTime = System.currentTimeMillis();
      counts = wordCounter.countOccurrencesInParallel(folder, args[1]);
      stopTime = System.currentTimeMillis();
      forkedThreadTimes[i] = (stopTime - startTime);
      System.out.println(counts + " , fork / join search took " + forkedThreadTimes[i] + "ms");
    }

    System.out.println("\nCSV Output:\n");
    System.out.println("Single thread,Fork/Join");
    for (int i = 0; i < repeatCount; i++) {
      System.out.println(singleThreadTimes[i] + "," + forkedThreadTimes[i]);
    }
    System.out.println();
  }
  @Test
  public void canCountNumbersOfLetters() throws Exception {
    String string = "abcd abc";

    WordCounter wc = new WordCounter(string);

    assertEquals(2, wc.getCountOf('a'));
    assertEquals(1, wc.getCountOf('d'));
    assertEquals(0, wc.getCountOf('z'));
  }
Example #3
0
 public static void main(String[] args) {
   Scanner scanner = new Scanner(System.in);
   String searched = scanner.next();
   char[][] map;
   int n, m;
   n = scanner.nextInt();
   m = scanner.nextInt();
   map = new char[n][m];
   for (int i = 0; i < n; i++) {
     for (int j = 0; j < m; j++) {
       map[i][j] = scanner.next().charAt(0);
     }
   }
   WordCounter counter = new WordCounter(map, searched);
   // Print result here
   System.out.println(counter.findTheWord());
 }
Example #4
0
 public static void main(String[] args) {
   ArrayList<String> al =
       new ArrayList<String>() {
         {
           add("a");
           add("aa");
           add("ab");
           add("ac");
           add("a_a");
           add("aa_a");
           add("ab_c");
           add("acc");
           add("aa");
           add("aa");
         }
       };
   WordCounter wordCounter = new WordCounter(al);
   System.out.println(wordCounter.howMahyTimes("aa"));
 }
 @Test
 public void testWeCanCountWords() {
   // GIVEN
   // a list of words (johan, bil, johan, hund, hus, johan, hund).
   List<String> words = new ArrayList<String>();
   words.add("johan");
   words.add("bil");
   words.add("johan");
   words.add("hund");
   words.add("hus");
   words.add("johan");
   words.add("hund");
   // WHEN
   // counting the word in the list
   Map<String, Integer> actualMap = wordCounter.countWordsInList(words);
   // THEN
   // we like to get a hashmap with 4 entries, one entry for each word in list
   Map<String, Integer> expectedMap = new HashMap<String, Integer>();
   expectedMap.put("johan", 4);
   expectedMap.put("bil", 1);
   expectedMap.put("hund", 2);
   expectedMap.put("hus", 1);
   assertEquals(expectedMap.size(), actualMap.size());
 }
  @Test
  public void shouldWorkWithEmptyStrings() {
    WordCounter wordCounter = new WordCounter("");

    assertEquals(0, wordCounter.getCountOf('a'));
  }