Skip to content

Instantly share code, notes, and snippets.

@glennpratt
Last active December 15, 2015 17:48
Show Gist options
  • Select an option

  • Save glennpratt/5298672 to your computer and use it in GitHub Desktop.

Select an option

Save glennpratt/5298672 to your computer and use it in GitHub Desktop.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Collection;
import java.util.HashSet;
public class WordOfTwo {
private Collection<String> dict = new HashSet<String>();
public Collection<String> find() {
Collection<String> ret = new HashSet<String>();
for (String word : dict) {
if (word.length() != 6)
continue;
for (int i = 2; i <= 4; i++) {
if (dict.contains(word.substring(0, i)) && dict.contains(word.substring(i))) {
ret.add(word);
break;
}
}
}
return ret;
}
public void loadFile(String path) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(path));
String aux;
while ((aux = reader.readLine()) != null) {
dict.add(aux);
}
}
public static void main(String[] args) {
for (int i = 0; i < 2; i++) {
test();
}
}
public static void test() {
WordOfTwo dictionary = new WordOfTwo();
long startTime = System.nanoTime();
try {
dictionary.loadFile("res/wordsEn.txt");
} catch (IOException e) {
throw new RuntimeException("Failed to open dictionary.", e);
}
long searchTime = System.nanoTime();
Collection<String> match = dictionary.find();
long endTime = System.nanoTime();
//System.out.println(match);
System.out.println("Load Time: " + (searchTime - startTime) / 1000000 + "ms");
System.out.println("Search Time: " + (endTime - searchTime) / 1000000 + "ms");
System.out.println("Total Time: " + (endTime - startTime) / 1000000 + "ms");
int expected = 3715;
int count = match.size();
System.out.println("Found: " + count + " words");
if (count != expected)
throw new RuntimeException("Expected " + expected + " matches, got " + count + " matches.");
}
}
@glennpratt
Copy link
Author

Read's faster once the file is cached:

Load Time: 0.068796
Search Time: 0.030156
Total Time: 0.098952
Load Time: 0.027102
Search Time: 0.025036
Total Time: 0.052138
Load Time: 0.034772
Search Time: 0.00759
Total Time: 0.042362
Load Time: 0.030268
Search Time: 0.00723
Total Time: 0.037498
Load Time: 0.033377
Search Time: 0.006703
Total Time: 0.04008
Load Time: 0.040017
Search Time: 0.005629
Total Time: 0.045646
Load Time: 0.025513
Search Time: 0.007767
Total Time: 0.03328
Load Time: 0.027963
Search Time: 0.007702
Total Time: 0.035665
Load Time: 0.026415
Search Time: 0.007613
Total Time: 0.034028
Load Time: 0.026203
Search Time: 0.007872
Total Time: 0.034075
Load Time: 0.034507
Search Time: 0.007146
Total Time: 0.041653
Load Time: 0.06376
Search Time: 0.007154
Total Time: 0.070914
Load Time: 0.026322
Search Time: 0.033975
Total Time: 0.060297
Load Time: 0.026814
Search Time: 0.009131
Total Time: 0.035945
Load Time: 0.026638
Search Time: 0.008183
Total Time: 0.034821
Load Time: 0.024653
Search Time: 0.011622
Total Time: 0.036275
Load Time: 0.029263
Search Time: 0.007243
Total Time: 0.036506
Load Time: 0.035769
Search Time: 0.007342
Total Time: 0.043111
Load Time: 0.044243
Search Time: 0.005369
Total Time: 0.049612
Load Time: 0.026526
Search Time: 0.015027
Total Time: 0.041553

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment