在多线程环境中.NET .NET通用集合是否较慢

时间:2022-06-11 00:58:54

Couple of days ago, I turned up for c#.net developer interview in stock based company where their application had to give frequent updates within second. So, Interviewer told me that acquiring lock or providing thread synchronization on .NET Generic collection like List, Stack, Dictionary is very much slow. So, they use their custom collection. So, I was wondering "Are .net collection really slow when acquiring locks and releasing locks and even if they are slow, then how we can improve that performance by writing custom generic classes"

几天前,我出现在股票公司的c#.net开发人员访谈中,他们的应用程序必须在第二秒内频繁更新。因此,Interviewer告诉我,获取锁定或在.NET Generic集合(如List,Stack,Dictionary)上提供线程同步非常慢。所以,他们使用他们的自定义集合。所以,我想知道“获取锁定和发布锁定时,.net集合真的很慢,即使它们很慢,那么我们如何通过编写自定义泛型类来提高性能”

1 个解决方案

#1


4  

Generics and multi-threading have nothing to do with each other. Given that, I'm not sure what you are asking.

泛型和多线程彼此无关。鉴于此,我不确定你在问什么。

Are .net collection really slow?

.net集合真的很慢吗?

...is unanswerable because performance is relative.

...是无法回答的,因为表现是相对的。

how we can improve that performance by writing custom generic classes

我们如何通过编写自定义泛型类来提高性能

You can't because generics have nothing to do with this. You can improve performance by writing custom collections that are tailored to the very specific needs of an application. It is rare that this is a good idea but it can be. For example it is easy to create a class that is faster than the built-in List<T>. Take List<T> as a template and remove all the iterator versioning logic to remove some overhead. This small win is rarely worth the cost.

你不能,因为泛型与此无关。您可以通过编写根据应用程序的特定需求定制的自定义集合来提高性能。这是一个好主意,但很少见。例如,创建一个比内置List 更快的类很容易。将List 作为模板并删除所有迭代器版本控制逻辑以消除一些开销。这个小小的胜利很少值得付出代价。

In case you want advice: Try to use the built-in collections. There's a System.Collections.Concurrent namespace for synchronized ones.

如果您需要建议:尝试使用内置集合。有同步的System.Collections.Concurrent命名空间。

Given the information that we have it is impossible to tell whether it was right or wrong for your interviewer to build custom collections.

鉴于我们掌握的信息,无法判断面试官是否对错构建自定义馆藏。

my question is that why lock is slower on .NET Collection

我的问题是为什么在.NET Collection上锁的速度较慢

You can only use lock with .NET so I'm not sure what you are asking here. Also: Slower than what?

你只能使用与.NET的锁,所以我不确定你在这里问什么。还有:比什么慢?

is there any way to achieve synchronization with mutable objects in a more faster way than what lock provides?

有没有办法以比锁提供更快的方式实现与可变对象的同步?

Often, that is possible. How this is done depends entirely on the concrete case. If there was a general way to do what lock does but faster then we would not need lock in the first place.

通常,这是可能的。如何完成这完全取决于具体案例。如果有一般方法来做锁定但速度更快,那么我们首先不需要锁定。

I'm trying to help you by collecting all the different questions you have asked and addressing them. I think if you had asked fewer, more precise questions you would have found the answer yourself or recognized that some questions do not make much sense. Asking the right question often leads to the answer.

我正在尝试通过收集您提出的所有不同问题并解决这些问题来帮助您。我想如果你问过更少,更准确的问题,你会自己找到答案,或者认识到某些问题没有多大意义。提出正确的问题通常会得出答案。

#1


4  

Generics and multi-threading have nothing to do with each other. Given that, I'm not sure what you are asking.

泛型和多线程彼此无关。鉴于此,我不确定你在问什么。

Are .net collection really slow?

.net集合真的很慢吗?

...is unanswerable because performance is relative.

...是无法回答的,因为表现是相对的。

how we can improve that performance by writing custom generic classes

我们如何通过编写自定义泛型类来提高性能

You can't because generics have nothing to do with this. You can improve performance by writing custom collections that are tailored to the very specific needs of an application. It is rare that this is a good idea but it can be. For example it is easy to create a class that is faster than the built-in List<T>. Take List<T> as a template and remove all the iterator versioning logic to remove some overhead. This small win is rarely worth the cost.

你不能,因为泛型与此无关。您可以通过编写根据应用程序的特定需求定制的自定义集合来提高性能。这是一个好主意,但很少见。例如,创建一个比内置List 更快的类很容易。将List 作为模板并删除所有迭代器版本控制逻辑以消除一些开销。这个小小的胜利很少值得付出代价。

In case you want advice: Try to use the built-in collections. There's a System.Collections.Concurrent namespace for synchronized ones.

如果您需要建议:尝试使用内置集合。有同步的System.Collections.Concurrent命名空间。

Given the information that we have it is impossible to tell whether it was right or wrong for your interviewer to build custom collections.

鉴于我们掌握的信息,无法判断面试官是否对错构建自定义馆藏。

my question is that why lock is slower on .NET Collection

我的问题是为什么在.NET Collection上锁的速度较慢

You can only use lock with .NET so I'm not sure what you are asking here. Also: Slower than what?

你只能使用与.NET的锁,所以我不确定你在这里问什么。还有:比什么慢?

is there any way to achieve synchronization with mutable objects in a more faster way than what lock provides?

有没有办法以比锁提供更快的方式实现与可变对象的同步?

Often, that is possible. How this is done depends entirely on the concrete case. If there was a general way to do what lock does but faster then we would not need lock in the first place.

通常,这是可能的。如何完成这完全取决于具体案例。如果有一般方法来做锁定但速度更快,那么我们首先不需要锁定。

I'm trying to help you by collecting all the different questions you have asked and addressing them. I think if you had asked fewer, more precise questions you would have found the answer yourself or recognized that some questions do not make much sense. Asking the right question often leads to the answer.

我正在尝试通过收集您提出的所有不同问题并解决这些问题来帮助您。我想如果你问过更少,更准确的问题,你会自己找到答案,或者认识到某些问题没有多大意义。提出正确的问题通常会得出答案。