Created
August 5, 2014 14:43
-
-
Save sue445/cc0b2624794dc169b6a7 to your computer and use it in GitHub Desktop.
HashMapとLinkedHashMapのベンチマーク
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* HashMapとLinkedHashMapの性能を調べるためのベンチマーク用のテストコード | |
* | |
* テスト内容 | |
* ・乱数を100万回putして実行時間を出力 | |
* ・乱数keyで100万回getして実行時間を出力 | |
* ・mapのサイズを取得して実行時間を出力 | |
* | |
* インスタンスのメモリサイズの取得にはClassmexerを使用しているため実行時に下記オプションをつけること | |
* -javaagent:./lib/classmexer.jar | |
* via. http://blogs.yahoo.co.jp/dk521123/32941391.html | |
*/ | |
import com.javamex.classmexer.MemoryUtil; | |
import org.junit.Before; | |
import org.junit.Test; | |
import java.io.ByteArrayOutputStream; | |
import java.io.IOException; | |
import java.io.ObjectOutputStream; | |
import java.util.HashMap; | |
import java.util.LinkedHashMap; | |
import java.util.Map; | |
import java.util.Random; | |
public class MapBenchmarkTest { | |
// Mapにputする回数 | |
private static final int MAX_PUT_COUNT = 1000000; | |
// Mapからgetする回数 | |
private static final int MAX_GET_COUNT = 1000000; | |
// 乱数の上限値 | |
private static final int MAX_RANDOM_VALUE = 10000000; | |
private Random random; | |
@Before | |
public void setUp(){ | |
random = new Random(); | |
} | |
@Test | |
public void benchmarkHashMap() throws Exception{ | |
System.out.println("[HashMap]"); | |
Map<Integer, Integer> target = new HashMap<Integer, Integer>(); | |
benchmarkMapPut(target); | |
benchmarkMapGet(target); | |
printObjectSize(target); | |
} | |
@Test | |
public void benchmarkLinkedHashMap() throws Exception{ | |
System.out.println("[LinkedHashMap]"); | |
Map<Integer, Integer> target = new LinkedHashMap<Integer, Integer>(); | |
benchmarkMapPut(target); | |
benchmarkMapGet(target); | |
printObjectSize(target); | |
} | |
// Map#putのベンチマーク(100万個の乱数をMapにputする) | |
private void benchmarkMapPut(Map<Integer, Integer> target){ | |
long startTime = System.currentTimeMillis(); | |
for(int i = 0; i < MAX_PUT_COUNT; i++){ | |
int key = random.nextInt(MAX_RANDOM_VALUE); | |
int value = random.nextInt(MAX_RANDOM_VALUE); | |
target.put(key, value); | |
} | |
long endTime = System.currentTimeMillis(); | |
System.out.println("put: " + (endTime - startTime) + " ms"); | |
} | |
// Map#getのベンチマーク(100万個の乱数をMapにputする) | |
private void benchmarkMapGet(Map<Integer, Integer> target){ | |
long startTime = System.currentTimeMillis(); | |
for(int i = 0; i < MAX_GET_COUNT; i++){ | |
int key = random.nextInt(MAX_RANDOM_VALUE); | |
target.get(key); | |
} | |
long endTime = System.currentTimeMillis(); | |
System.out.println("get: " + (endTime - startTime) + " ms"); | |
} | |
private void printObjectSize(Object target) throws IOException { | |
long memorySize = MemoryUtil.deepMemoryUsageOf(target); | |
System.out.println("memory size: " + memorySize + " bytes"); | |
// byte[] array = serialize(target); | |
// System.out.println("object size: " + array.length + " bytes"); | |
} | |
private byte[] serialize(Object src) throws IOException { | |
try( | |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
ObjectOutputStream oos = new ObjectOutputStream(baos); | |
) { | |
oos.writeObject(src); | |
oos.flush(); | |
return baos.toByteArray(); | |
} | |
} | |
} |
実行環境
$ java -version
openjdk version "1.7.0-u10-b09"
OpenJDK Runtime Environment (build 1.7.0-u10-b09-20121015)
OpenJDK 64-Bit Server VM (build 23.6-b03, mixed mode)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
実行結果
1回目
[HashMap]
put: 1697 ms
get: 197 ms
memory size: 69309168 bytes
[LinkedHashMap]
put: 1872 ms
get: 224 ms
memory size: 76898416 bytes
2回目
[HashMap]
put: 1611 ms
get: 196 ms
memory size: 69292864 bytes
[LinkedHashMap]
put: 2140 ms
get: 208 ms
memory size: 76898808 bytes
3回目
[HashMap]
put: 1797 ms
get: 214 ms
memory size: 69289488 bytes
[LinkedHashMap]
put: 444 ms
get: 222 ms
memory size: 76908392 bytes
4回目
[HashMap]
put: 1679 ms
get: 207 ms
memory size: 69293808 bytes
[LinkedHashMap]
put: 395 ms
get: 216 ms
memory size: 76910960 bytes
5回目
[HashMap]
put: 1729 ms
get: 206 ms
memory size: 69308512 bytes
[LinkedHashMap]
put: 435 ms
get: 212 ms
memory size: 76901160 bytes
6回目
[HashMap]
put: 2015 ms
get: 403 ms
memory size: 69311728 bytes
[LinkedHashMap]
put: 513 ms
get: 3307 ms
memory size: 76899928 bytes
7回目
[HashMap]
put: 1688 ms
get: 201 ms
memory size: 69296896 bytes
[LinkedHashMap]
put: 2194 ms
get: 306 ms
memory size: 76894976 bytes
8回目
[HashMap]
put: 1683 ms
get: 200 ms
memory size: 69326128 bytes
[LinkedHashMap]
put: 404 ms
get: 226 ms
memory size: 76889544 bytes
9回目
[HashMap]
put: 1692 ms
get: 207 ms
memory size: 69330912 bytes
[LinkedHashMap]
put: 425 ms
get: 258 ms
memory size: 76902552 bytes
10回目
[HashMap]
put: 1729 ms
get: 206 ms
memory size: 69304816 bytes
[LinkedHashMap]
put: 405 ms
get: 214 ms
memory size: 76917440 bytes
10回の平均値
[HashMap]
put: 1732.0 ms
get: 223.7 ms
memory size: 69306432 bytes
[LinkedHashMap]
put: 922.7 ms
get: 517.9 ms
memory size: 76902217.6 bytes
HashMap - LinkedHashMap
(1732.0 + 223.7) - (922.7 + 517.9) = 515.ms
69306432 - 76902217.6 = -7595785.599999994 bytes = -7.243905639648432 MB