zoukankan      html  css  js  c++  java
  • Performance Test of List<T>, LinkedList<T>, Queue<T>, ConcurrentQueue<T>

                //Test Group 1
                {
                    var watch = Stopwatch.StartNew();
                    var list = new List<int>();
                    for (int j = 0; j < 300000; j++)
                    {
                        list.Insert(0, j);
                    }
                    watch.Stop();
                    Console.WriteLine("Insert First::List::" + watch.ElapsedMilliseconds);
                }
    
                {
                    var watch = Stopwatch.StartNew();
                    var list = new LinkedList<int>();
                    for (int j = 0; j < 300000; j++)
                    {
                        list.AddFirst(j);
                    }
                    watch.Stop();
                    Console.WriteLine("Insert First::LinkedList::" + watch.ElapsedMilliseconds);
                }
    
                //Test Group 2:
                {
                    var watch = Stopwatch.StartNew();
                    var list = new List<int>();
                    for (int j = 0; j < 300000; j++)
                    {
                        list.Add(j);
                    }
                    watch.Stop();
                    Console.WriteLine("Append::List::" + watch.ElapsedMilliseconds);
                }
    
                {
                    var watch = Stopwatch.StartNew();
                    var list = new LinkedList<int>();
                    for (int j = 0; j < 300000; j++)
                    {
                        list.AddLast(j);
                    }
                    watch.Stop();
                    Console.WriteLine("Append::LinkedList::" + watch.ElapsedMilliseconds);
                }
    
                {
                    var watch = Stopwatch.StartNew();
                    var queue = new Queue<int>();
                    for (int j = 0; j < 300000; j++)
                    {
                        queue.Enqueue(j);
                    }
                    watch.Stop();
                    Console.WriteLine("Enqueue::Queue::" + watch.ElapsedMilliseconds);
                }
    
                {
                    var watch = Stopwatch.StartNew();
                    var queue = new ConcurrentQueue<int>();
                    for (int j = 0; j < 300000; j++)
                    {
                        queue.Enqueue(j);
                    }
                    watch.Stop();
                    Console.WriteLine("Enqueue::ConcurrentQueue::" + watch.ElapsedMilliseconds);
                }
    
                //Test Group 3:
                {
                    var list = new List<int>();
                    for (int j = 0; j < 300000; j++)
                    {
                        list.Add(j);
                    }
    
                    var watch = Stopwatch.StartNew();
                    for (int j = 0; j < 300000; j++)
                    {
                        var value = list[0];
                        list.RemoveAt(0);
                    }
                    watch.Stop();
                    Console.WriteLine("RemoveAt(0)::List::" + watch.ElapsedMilliseconds);
                }
    
                {
    
                    var list = new LinkedList<int>();
                    for (int j = 0; j < 300000; j++)
                    {
                        list.AddLast(j);
                    }
    
                    var watch = Stopwatch.StartNew();
                    for (int j = 0; j < 300000; j++)
                    {
                        var value = list.First.Value;
                        list.RemoveFirst();
                    }
                    watch.Stop();
                    Console.WriteLine("RemoveFirst::LinkedList::" + watch.ElapsedMilliseconds);
                }
    
                {
                    var queue = new Queue<int>();
                    for (int j = 0; j < 300000; j++)
                    {
                        queue.Enqueue(j);
                    }
    
                    var watch = Stopwatch.StartNew();
                    for (int j = 0; j < 300000; j++)
                    {
                        var value = queue.Dequeue();
                    }
                    watch.Stop();
                    Console.WriteLine("Dequeue::Queue::" + watch.ElapsedMilliseconds);
                }
    
                {
                    var queue = new ConcurrentQueue<int>();
                    for (int j = 0; j < 300000; j++)
                    {
                        queue.Enqueue(j);
                    }
    
                    var watch = Stopwatch.StartNew();
                    for (int j = 0; j < 300000; j++)
                    {
                        int value;
                        queue.TryDequeue(out value);
                    }
                    watch.Stop();
                    Console.WriteLine("TryDequeue::ConcurrentQueue::" + watch.ElapsedMilliseconds);
                }

    List::Insert First::57328 ms
    List::Add::8 ms
    List::RemoveAt(0)::56700 ms


    LinkedList::AddFirst::34 ms
    LinkedList::Append::100 ms
    LinkedList::RemoveFirst::16 ms

    Queue::Enqueue::15 ms
    Queue::Dequeue::10 ms

    ConcurrentQueue::Enqueue::138 ms
    ConcurrentQueue::TryDequeue::18 ms

  • 相关阅读:
    看懂SqlServer查询计划
    jQuery 插件autocomplete自动完成应用(自动补全)(asp.net后台)
    MVC Html.AntiForgeryToken() 防止CSRF攻击
    iOS开发UI篇—transframe属性(形变)
    iOS开发UI基础—手写控件,frame,center和bounds属性
    iOS开发UI篇—Button基础
    iOS开发UI篇—UITableviewcell的性能优化和缓存机制
    iOS开发UI篇—UITableview控件基本使用
    iOS开发UI篇—UITableview控件简单介绍
    iOS开发UI篇—推荐两个好用的Xcode插件(提供下载链接)
  • 原文地址:https://www.cnblogs.com/zanxiaofeng/p/3955964.html
Copyright © 2011-2022 走看看