本文介绍了ConcurrentBag< T>和lock(List< T>)添加或删除哪个更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 List< T> 上执行一些线程安全操作。通常我只是简单地使用:

I need to perform some thread safe operations on List<T>. Usually I just simply use:

lock(List<T>)
{
   List<T>.Add();
   List<T>.Remove();
}

我知道还有另一种方法,使用 ConcurrentBag< T> 。但是我不知道哪个更快,或者有其他区别。

I know there is another way, using ConcurrentBag<T>. But I don't know which is faster, or any other differences.

编辑:

有人建议我使用 ConcurrentBag ,因为这样做更安全。但是我担心这会使我的操作变慢。

Some people just recommend I use ConcurrentBag, because that's safer. But I worry it will make my operation slower.

我有很多线程需要从 List< T> ,我想知道哪种方法对性能更好。

I have a lot of threads needing to add or remove objects from a List<T>, I want to know which way is better for performance.

推荐答案

您可以轻松衡量尝试一下就可以采用不同的方法!那就是我刚刚得到的:

You can easily measure the performance of different approaches just by trying them out! That is what I just got:

lock list: 2.162s
ConcurrentBag: 7.264s



using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

public class Test
{
    public const int NumOfTasks = 4;
    public const int Cycles = 1000 * 1000 * 4;

    public static void Main()
    {
        var list = new List<int>();
        var bag = new ConcurrentBag<int>();

        Profile("lock list", () => { lock (list) list.Add(1); });
        Profile("ConcurrentBag", () => bag.Add(1));
    }

    public static void Profile(string label, Action work)
    {
        var s = new Stopwatch();
        s.Start();

        List<Task> tasks = new List<Task>();

        for (int i = 0; i < NumOfTasks; ++i)
        {
            tasks.Add(Task.Factory.StartNew(() =>
            {
                for (int j = 0; j < Cycles; ++j)
                {
                    work();
                }
            }));
        }

        Task.WaitAll(tasks.ToArray());

        Console.WriteLine(string.Format("{0}: {1:F3}s", label, s.Elapsed.TotalSeconds));
    }
}

这篇关于ConcurrentBag&lt; T&gt;和lock(List&lt; T&gt;)添加或删除哪个更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 01:08