I have a pretty big Hashmap (~ 250 MB). Creation takes about 50-55 seconds, so I decided to serialize it and save it to a file. Reading from a file takes about 16-17 seconds.
The only problem is that the search looks slower. I always thought that the hash of the file is read from the file into memory, so the performance should be the same as when I create the hash map myself, right? Here is the code that I use to read hashmap into a file:
File file = new File("omaha.ser"); FileInputStream f = new FileInputStream(file); ObjectInputStream s = new ObjectInputStream(new BufferedInputStream(f)); omahaMap = (HashMap<Long, Integer>) s.readObject(); s.close();
300 million search queries take about 3.1 seconds when I create a hash map myself, and about 8.5 seconds when I read the same hash file from a file. Does anyone have an idea why? I do not notice anything obvious?
EDIT:
I โmeasuredโ time by simply taking time with System.nanotime (), so the proper comparison method was not used. Here is the code:
public class HandEvaluationTest { public static void Test() { HandEvaluation.populate5Card(); HandEvaluation.populate9CardOmaha(); Card[] player1cards = {new Card("4s"), new Card("2s"), new Card("8h"), new Card("4d")}; Card[] player2cards = {new Card("As"), new Card("9s"), new Card("6c"), new Card("2h")}; Card[] player3cards = {new Card("9h"), new Card("7h"), new Card("Kc"), new Card("Kh")}; Card[] table = {new Card("2d"), new Card("2c"), new Card("3c"), new Card("5c"), new Card("4h")}; int j=0, k=0, l=0; long startTime = System.nanoTime(); for(int p=0; p<100000000; p++) { j = HandEvaluation.handEval9Hash(player1cards, table); k = HandEvaluation.handEval9Hash(player2cards, table); l = HandEvaluation.handEval9Hash(player3cards, table); } long estimatedTime = System.nanoTime() - startTime; System.out.println("Time needed: " + estimatedTime*Math.pow(10,-6) + "ms"); System.out.println("Handstrength Player 1: " + j); System.out.println("Handstrength Player 2: " + k); System.out.println("Handstrength Player 3: " + l); } }
A lot of hashmap work is done in HandEvaluation.populate9CardOmaha (). 5-card small. Code for the big one:
public static void populate9CardOmaha() { //Check if the hashmap is already there- then just read it and exit File hashmap = new File("omaha.ser"); if(hashmap.exists()) { try { File file = new File("omaha.ser"); FileInputStream f = new FileInputStream(file); ObjectInputStream s = new ObjectInputStream(new BufferedInputStream(f)); omahaMap = (HashMap<Long, Integer>) s.readObject(); s.close(); } catch(IOException ioex) {ioex.printStackTrace();} catch(ClassNotFoundException cnfex) { System.out.println("Class not found"); cnfex.printStackTrace(); return; } return; } // if it not there, populate it yourself ... Code for populating hashmap ... // and then save it to file ( try { File file = new File("omaha.ser"); FileOutputStream f = new FileOutputStream(file); ObjectOutputStream s = new ObjectOutputStream(new BufferedOutputStream(f)); s.writeObject(omahaMap); s.close(); } catch(IOException ioex) {ioex.printStackTrace();} }
When I fill it out (= the file is not here), the search in HandEvaluationTest.Test () takes about 8 seconds instead of 3. Maybe this is just my most naive way of measuring elapsed time?