Skip to content

Instantly share code, notes, and snippets.

@neiraza
Created September 18, 2014 15:33
Show Gist options
  • Save neiraza/4505f98a35093047c5fa to your computer and use it in GitHub Desktop.
Save neiraza/4505f98a35093047c5fa to your computer and use it in GitHub Desktop.
01:import java.util.Hashtable;
02:
03:public class HashSearch {
04:
05: public static int exec(Hashtable vals, int target) {
06: if (vals.containsKey(target)) {
07: return (Integer) vals.get(target);
08: } else {
09: return -1;
10: }
11: }// end of exec
12:
13: public static void main(String[] args) {
14: int len = 100000;
15: int[] data;
16: data = new int[len];
17: for(int i=0; i<len; ++i) data[i]=i;
18: Hashtable <Integer, Integer>hasharray = new Hashtable<Integer, Integer>();
19: for (int i=0; i<data.length; ++i){
20: hasharray.put(data[i],i);
21: }
22: int result;
23: int repeat = 10000;
24: long time;
25: time = System.currentTimeMillis();
26: for (int i=0; i<repeat; ++i){
27: HashSearch.exec(hasharray,3);
28: HashSearch.exec(hasharray,27);
29: HashSearch.exec(hasharray,(int)(len/2));
30: HashSearch.exec(hasharray,(int)(len*2));
31: }
32: time = System.currentTimeMillis() - time;
33: System.out.println(time + "[msec]passed.");
34: }// end of main
35:
36:
37: private static void resultPrint(int result){
38: if (result != -1) {
39: System.out.println("[ Found ] index key = " + result);
40: } else {
41: System.out.println("[Not Found]");
42: }
43: }// end of resultPrint
44:
45:}// end of HashSearch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment