view src/test/MapBenchmark.java @ 0:a9cb12a7f995

hg init
author misaka
date Wed, 06 Jul 2011 15:19:52 +0900
parents
children
line wrap: on
line source

package test;

import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import tree.*;

public class MapBenchmark
{
	public static void main(String args[]) throws InterruptedException
	{
		int num = 20000;
		int threads = 5;
		ExecutorService service = Executors.newFixedThreadPool(threads);
		
		Map<Integer,Integer> map = new SynchronizedMap<Integer,Integer>(new AVLTreeMap<Integer,Integer>());
		//Map<Integer,Integer> map = Collections.synchronizedMap(new TreeMap<Integer,Integer>());
		
		
		LinkedList<Integer> keys[] = new LinkedList[threads];
		for(int i = 0;i < keys.length;i ++){
			Random rnd = new Random();
			keys[i] = new LinkedList<Integer>();
			for(int j = 0;j < num;j ++){
				keys[i].add(rnd.nextInt());
			}
		}
		
		Task<Integer> tasks[] = new Task[threads];
		for(int i = 0;i < tasks.length;i ++){
			tasks[i] = new Task<Integer>(keys[i],map);
		}
		
		for(int i = 0;i < tasks.length;i ++){
			service.execute(tasks[i]);
		}
		
		service.shutdown();
		service.awaitTermination(Long.MAX_VALUE,TimeUnit.DAYS);
		
		System.out.println("");
	}
	
	public static class Task<K> implements Runnable
	{
		private Map<K,K> map;
		private List<K> keys;
		public Task(List<K> keys,Map<K,K> map)
		{
			this.keys = keys;
			this.map = map;
		}
		
		@Override
		public void run()
		{
			long start = System.currentTimeMillis();
			for(K key : keys){
				map.put(key,key);
			}
			System.out.println(System.currentTimeMillis() - start);
		}
	}
}