Mercurial > hg > Members > shoshi > AADS
comparison src/test/MapBenchmark.java @ 0:a9cb12a7f995
hg init
author | misaka |
---|---|
date | Wed, 06 Jul 2011 15:19:52 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:a9cb12a7f995 |
---|---|
1 package test; | |
2 | |
3 import java.util.LinkedList; | |
4 import java.util.List; | |
5 import java.util.Random; | |
6 import java.util.concurrent.ExecutorService; | |
7 import java.util.concurrent.Executors; | |
8 import java.util.concurrent.TimeUnit; | |
9 import tree.*; | |
10 | |
11 public class MapBenchmark | |
12 { | |
13 public static void main(String args[]) throws InterruptedException | |
14 { | |
15 int num = 20000; | |
16 int threads = 5; | |
17 ExecutorService service = Executors.newFixedThreadPool(threads); | |
18 | |
19 Map<Integer,Integer> map = new SynchronizedMap<Integer,Integer>(new AVLTreeMap<Integer,Integer>()); | |
20 //Map<Integer,Integer> map = Collections.synchronizedMap(new TreeMap<Integer,Integer>()); | |
21 | |
22 | |
23 LinkedList<Integer> keys[] = new LinkedList[threads]; | |
24 for(int i = 0;i < keys.length;i ++){ | |
25 Random rnd = new Random(); | |
26 keys[i] = new LinkedList<Integer>(); | |
27 for(int j = 0;j < num;j ++){ | |
28 keys[i].add(rnd.nextInt()); | |
29 } | |
30 } | |
31 | |
32 Task<Integer> tasks[] = new Task[threads]; | |
33 for(int i = 0;i < tasks.length;i ++){ | |
34 tasks[i] = new Task<Integer>(keys[i],map); | |
35 } | |
36 | |
37 for(int i = 0;i < tasks.length;i ++){ | |
38 service.execute(tasks[i]); | |
39 } | |
40 | |
41 service.shutdown(); | |
42 service.awaitTermination(Long.MAX_VALUE,TimeUnit.DAYS); | |
43 | |
44 System.out.println(""); | |
45 } | |
46 | |
47 public static class Task<K> implements Runnable | |
48 { | |
49 private Map<K,K> map; | |
50 private List<K> keys; | |
51 public Task(List<K> keys,Map<K,K> map) | |
52 { | |
53 this.keys = keys; | |
54 this.map = map; | |
55 } | |
56 | |
57 @Override | |
58 public void run() | |
59 { | |
60 long start = System.currentTimeMillis(); | |
61 for(K key : keys){ | |
62 map.put(key,key); | |
63 } | |
64 System.out.println(System.currentTimeMillis() - start); | |
65 } | |
66 } | |
67 } |