4

最近,我需要在使用SortedDictionaryand之间做出选择SortedList,并选择了SortedList.

但是,现在我发现我的 C# 程序在执行 SortedList.Count 时速度变慢了,我使用调用了数千次的函数/方法对其进行了检查。

通常我的程序会在 35 毫秒内调用该函数 10,000 次,但是在使用SortedList.Count时,它会减慢到 300-400 毫秒,基本上慢了 10 倍。

我也尝试过SortedList.Keys.Count,但这又将我的性能降低了 10 倍,超过 3000 毫秒。

I have only ~5000 keys/objects in SortedList<DateTime, object_name>. 我可以轻松地立即从排序列表中检索数据SortedList[date] (in 35 ms),因此我没有发现列表结构或其持有的对象有任何问题。

这种表现正常吗?

我还能使用什么来获取列表中的记录数,或者只是检查列表是否已填充?(除了添加一个单独的跟踪标志,我现在可以这样做)

更正:对不起,我实际上在使用: ConcurrentDictionary<string, SortedList<DateTime, string>> dict_list = new ConcurrentDictionary<string, SortedList<DateTime, string>>(); 而且我在各个地方都有各种计数,有时检查列表中的项目,有时检查 ConcurrentDicitonary 中的项目。所以这个问题适用于 ConcurrentDicitonary,我编写了快速测试代码来确认这一点,这需要 350 毫秒,而不使用并发。这是使用 ConcurrentDicitonary 进行的测试,显示 350 毫秒:

public static void CountTest()
{
    //Create test ConcurrentDictionary
    ConcurrentDictionary<int, string> test_dict = new ConcurrentDictionary<int, string>();
    for (int i = 0; i < 50000; i++)
    {
        test_dict[i] = "ABC";
    }

    //Access .Count property 10,000 times
    int tick_count = Environment.TickCount;
    for (int i = 1; i <= 10000; i++)
    {
        int dict_count = test_dict.Count;
    }

    Console.WriteLine(string.Format("Time: {0} ms", Environment.TickCount - tick_count));
    Console.ReadKey();
}
4

3 回答 3

10

本文建议改为调用它

dictionary.Skip(0).Count()

一旦来自该方法的调用返回,计数就可能无效。例如,如果要将计数写入日志以进行跟踪,则可以使用其他方法,例如无锁枚举器

于 2018-03-07T10:34:40.773 回答
2

好吧,ConcurrentDictionary<TKey, TValue>必须同时与许多线程一起正常工作,所以它需要一些同步开销。

属性的源代码Counthttps ://referencesource.microsoft.com/#mscorlib/system/Collections/Concurrent/ConcurrentDictionary.cs,40c23c8c36011417

    public int Count
    {
        [SuppressMessage("Microsoft.Concurrency", "CA8001", Justification = "ConcurrencyCop just doesn't know about these locks")]
        get
        {
            int count = 0;

            int acquiredLocks = 0;
            try
            {
                // Acquire all locks
                AcquireAllLocks(ref acquiredLocks);

                // Compute the count, we allow overflow
                for (int i = 0; i < m_tables.m_countPerLock.Length; i++)
                {
                    count += m_tables.m_countPerLock[i];
                }

            }
            finally
            {
                // Release locks that have been acquired earlier
                ReleaseLocks(0, acquiredLocks);
            }

            return count;
        }
    }

看起来您需要重构现有代码。由于您没有提供任何代码,我们无法告诉您可以优化什么。

于 2016-12-23T10:05:34.793 回答
0

For performance sensitive code I would not recommend to use ConcurrentDictionary.Count property as it has a locking implementation. You could use Interlocked.Increment instead to do the count yourself.

于 2017-10-24T17:22:44.420 回答