【愚公系列】2021年11月 C#版 数据结构与算法解析(队列)

2021-12-03 16:30:47 浏览数 (1)

代码语言:javascript复制
public void Queue()
{
    var test = new ConcurrentQueue<TestModel>();//安全队列
    //var test = Channel.CreateBounded(int.MaxValue);//管道
    //var test = new Stack();//栈
    //var test = new Queue();//队列
    for (int i = 0; i < 1_000_000; i  )
    {
        test.Enqueue(new TestModel());
        //test.Writer.TryWrite(new TestModel()); 
        //test.Push(new TestModel());
        //test.Enqueue(new TestModel()); 
    } 
}

public void QueueIO()
{
    var test = new ConcurrentQueue<TestModel>();
    //var test = Channel.CreateBounded(int.MaxValue);
    //var test = new Stack();
    //var test = new Queue();
    for (int i = 0; i < 1_000_000; i  )
    {
        test.Enqueue(new TestModel());
        //test.Writer.TryWrite(new TestModel()); 
        //test.Push(new TestModel()); 
        //test.Enqueue(new TestModel()); 
    }

    for (int i = 0; i < 1_000_000; i  )
    {
        test.TryDequeue(out var _);
        //test.Reader.TryRead(out var _);
        //test.Pop();
        //test.Dequeue();
    }
}

ConcurrentQueue和Queue的性能差别是纳米级大部分情况下都是使用ConcurrentQueue

0 人点赞