ConcurrentSkipListMap性能测试

本文通过实验对比了ConcurrentMap、ConcurrentSkipListMap和ConcurrentLinkedDeque三种并发集合在加载和运行过程中的性能表现,揭示了不同场景下各集合的优势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package vertices;

import java.util.Map;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentSkipListMap;
import org.apache.hadoop.io.FloatWritable;
import org.apache.hadoop.io.IntWritable;
import com.google.common.collect.Maps;

/**
 * 注意 :ConcurrentSkipListMap和ConcurrentLinkedDeque都可以双向访问, ConcurrentSkipListMap可以将keyset进行降序遍历,ConcurrentLinkedDeque提供降序迭代器
 * @author Administrator
 *
 */
public class VerticesTest {

	public static void main(String[] args) {
		int VERTICES_NUMBER_0 =  400000;
		int VERTICES_NUMBER_1 = 1000000;

        //case1
		long start = System.currentTimeMillis();
		 ConcurrentMap<IntWritable, TestVertex> vertexMap0 = Maps
		 .newConcurrentMap();
		 ConcurrentMap<IntWritable, TestVertex> vertexMap1 = Maps
		 .newConcurrentMap();
		 for (int i = 0; i < VERTICES_NUMBER_0; i++) {
		 vertexMap0.put(new IntWritable(i), new TestVertex(
		 new IntWritable(i), new FloatWritable(i)));
		 }
		 for (int i = 0; i < VERTICES_NUMBER_1; i++) {
		 vertexMap1.put(new IntWritable(i), new TestVertex(
		 new IntWritable(i), new FloatWritable(i)));
		 }
		 System.out.println("ConcurrentMap load last "
		 + (System.currentTimeMillis() - start) + " ms");
		
		 start = System.currentTimeMillis();
		 Thread t0 = new Thread(new MapWorker(vertexMap0));
		 Thread t1 = new Thread(new MapWorker(vertexMap1)) ;
		 try {
		 t0.start() ;
		 t1.start();
		 t0.join() ;
		 t1.join();
		 } catch (InterruptedException e) {
		 e.printStackTrace();
		 }
		 System.out.println("ConcurrentMap run last "
		 + (System.currentTimeMillis() - start) + " ms");

         //case2
		 start = System.currentTimeMillis();
		 ConcurrentSkipListMap<IntWritable, TestVertex> skipMap0 = new ConcurrentSkipListMap<IntWritable, TestVertex> () ;
		 ConcurrentSkipListMap<IntWritable, TestVertex> skipMap1 = new ConcurrentSkipListMap<IntWritable, TestVertex> () ;
		 for (int i = 0; i < VERTICES_NUMBER_0; i++) {
			 skipMap0.put(new IntWritable(i), new TestVertex(
		 new IntWritable(i), new FloatWritable(i)));
		 }
		 for (int i = 0; i < VERTICES_NUMBER_1; i++) {
			 skipMap1.put(new IntWritable(i), new TestVertex(
		 new IntWritable(i), new FloatWritable(i)));
		 }
		 System.out.println("ConcurrentSkipListMap load last "
		 + (System.currentTimeMillis() - start) + " ms");
		
		 start = System.currentTimeMillis();
		 t0 = new Thread(new MapWorker(skipMap0));
		 t1 = new Thread(new MapWorker(skipMap1)) ;
		 try {
		 t0.start() ;
		 t1.start();
		 t0.join() ;
		 t1.join();
		 } catch (InterruptedException e) {
		 e.printStackTrace();
		 }
		 System.out.println("ConcurrentSkipListMap run last "
		 + (System.currentTimeMillis() - start) + " ms");
		
	    //case3
		ConcurrentLinkedDeque<TestVertex> vertices0 = new ConcurrentLinkedDeque<TestVertex>();
		ConcurrentLinkedDeque<TestVertex> vertices1 = new ConcurrentLinkedDeque<TestVertex>();
		start = System.currentTimeMillis();
		for (int i = 0; i < VERTICES_NUMBER_0; i++) {
			vertices0.add(new TestVertex(new IntWritable(i), new FloatWritable(
					i)));
		}
		for (int i = 0; i < VERTICES_NUMBER_1; i++) {
			vertices1.add(new TestVertex(new IntWritable(i), new FloatWritable(
					i)));
		}
		System.out.println("ConcurrentLinkedDeque Load last "
				+ (System.currentTimeMillis() - start) + " ms");

		start = System.currentTimeMillis();
		t0 = new Thread(new DequeWorker(vertices0));
		t1 = new Thread(new DequeWorker(vertices1));
		try {
			t0.start();
			t1.start();
			t0.join();
			t1.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("ConcurrentLinkedDeque run last "
				+ (System.currentTimeMillis() - start) + " ms");
		System.out.println(vertices1.getLast().value);
	}
}

class TestVertex {
	IntWritable id;
	FloatWritable value;
	int flag = 0;

	public TestVertex(IntWritable id, FloatWritable value) {
		this.id = id;
		this.value = value;
	}
}

class MapWorker implements Runnable {
	ConcurrentMap<IntWritable, TestVertex> vertexMap;

	public MapWorker(ConcurrentMap<IntWritable, TestVertex> vertexMap) {
		this.vertexMap = vertexMap;
	}

	@Override
	public void run() {
		for (Map.Entry<IntWritable, TestVertex> e : vertexMap.entrySet()) {
			TestVertex v = e.getValue();
			v.value.set(v.value.get() * 2);
		}
	}
}

class DequeWorker implements Runnable {
	ConcurrentLinkedDeque<TestVertex> vertices;

	public DequeWorker(ConcurrentLinkedDeque<TestVertex> vertices) {
		this.vertices = vertices;
	}

	@Override
	public void run() {
//		long start = System.currentTimeMillis();
		for (TestVertex v : vertices) {
			v.value.set(v.value.get() * 2);
			v.flag = 1 ;
		}
//		System.out.println(Thread.currentThread().getName() + " last " + (System.currentTimeMillis() - start) + " ms" );
	}
}


测试结果:

ConcurrentMap load last 1633 ms
ConcurrentMap run last 103 ms
ConcurrentSkipListMap load last 1178 ms
ConcurrentSkipListMap run last 38 ms
ConcurrentLinkedDeque Load last 122 ms
ConcurrentLinkedDeque run last 21 ms


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值