主要是为了选择快速的Map类库,系统自带的貌似很慢

测试代码

import java.util.Collections;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import javolution.util.FastMap;
import org.junit.Test;
import com.alisoft.nano.bench.Nano;
public class MapBenchTest {
    private static int measurements = 100000;
    private static int threads = 200;

    @Test
    public void testHashTable() {
        final Hashtable hash = new Hashtable();
        Nano.bench().measurements(measurements).threads(threads).measure(
                "hashtable", new Runnable() {
            public void run() {
                Random random = new Random(measurements);
                for (int i = 0; i < 1000; i++) {
                    hash.put(i, i);
                    if (i % 2 == 0) {
                        for (int j = 0; j < 3; j++) {
                            hash.get(random.nextInt());
                        }
                    }
                }
            }
        });
    }
    @Test
    public void testSyncHashMap() {
        final Map hash = Collections
                .synchronizedMap(new HashMap());
        Nano.bench().measurements(measurements).threads(threads).measure(
                "sync-hashmap", new Runnable() {
            public void run() {
                Random random = new Random(measurements);
                for (int i = 0; i < 1000; i++) {
                    hash.put(i, i);
                    if (i % 2 == 0) {
                        for (int j = 0; j < 3; j++) {
                            hash.get(random.nextInt());
                        }
                    }
                }
            }
        });
    }
    @Test
    public void testCurrentHashMap() {
        final Map hash = new ConcurrentHashMap();
        Nano.bench().measurements(measurements).threads(threads).measure(
                "concurrent-hashmap", new Runnable() {
            public void run() {
                Random random = new Random(measurements);
                for (int i = 0; i < 1000; i++) {
                    hash.put(i, i);
                    if (i % 2 == 0) {
                        for (int j = 0; j < 3; j++) {
                            hash.get(random.nextInt());
                        }
                    }
                }
            }
        });
    }
    @Test
    public void testCurrentFashMap() {
        final Map hash = new FastMap();
        Nano.bench().measurements(measurements).threads(threads).measure(
                "javolution-fastmap", new Runnable() {
            public void run() {
                Random random = new Random(measurements);
                for (int i = 0; i < 1000; i++) {
                    hash.put(i, i);
                    if (i % 2 == 0) {
                        for (int j = 0; j < 3; j++) {
                            hash.get(random.nextInt());
                        }
                    }
                }
            }
        });
    }
}

测试环境

OS
fedora 14 64bit
JDK
Java HotSpot(TM) 64-Bit Server VM (build 17.1-b03, mixed mode)

测试结果

Avg(ms) Total(ms)   tps memory-usage(Kb)
hashtable   0.25    25222.81    3964.67 117520.44
sync-hashmap    0.29    28721.67    3481.69 59467.304
concurrent-hashmap  0.07    7444.2  13433.27    103376.848
javolution-fastmap  0.03    3433.57 29124.17    124120.56

Comments