I have a pretty large Hashmap (~250MB). Creating it takes about 50-55 seconds, so I decided to serialize it and save it to a file. Reading from the file takes about 16-17 se
This question was interesting, so I wrote my own test case to verify it. I found no difference in speed for a live lookup Vs one that was loaded from a serialized file. The program is available at the end of the post for anyone interested in running it.
230 MB
.Java(TM) SE Runtime Environment (build 1.6.0_25-b06)
/ 4 CPUs running at 1.7 Ghz
/ 4GB Ram 800 Mhz
Measuring is tricky. I myself noticed the 8 second
lookup time that you described, but guess what else I noticed when that happened.
Your measurements are probably picking that up too. If you isolate the measurements of Map.get()
alone you'll see that the results are comparable.
public class GenericTest
{
public static void main(String... args)
{
// Call the methods as you please for a live Vs ser <-> de_ser run
}
private static Map generateHashMap()
{
Map map = new HashMap();
final Random random = new Random();
for(int counter = 0 ; counter < 10000000 ; counter++)
{
final int value = random.nextInt();
final long key = random.nextLong();
map.put(key, value);
}
return map;
}
private static void lookupItems(int n, Map map)
{
final Random random = new Random();
for(int counter = 0 ; counter < n ; counter++)
{
final long key = random.nextLong();
final Integer value = map.get(key);
}
}
private static void serialize(Map map)
{
try
{
File file = new File("temp/omaha.ser");
FileOutputStream f = new FileOutputStream(file);
ObjectOutputStream s = new ObjectOutputStream(new BufferedOutputStream(f));
s.writeObject(map);
s.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
private static Map deserialize()
{
try
{
File file = new File("temp/omaha.ser");
FileInputStream f = new FileInputStream(file);
ObjectInputStream s = new ObjectInputStream(new BufferedInputStream(f));
HashMap map = (HashMap) s.readObject();
s.close();
return map;
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
}