mardi 8 avril 2014

c# - les collections génériques de .NET sont plus lentes en multi threading environnement - Stack Overflow


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"




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?



...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.


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


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



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



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.



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"



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?



...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.


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


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



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



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.


0 commentaires:

Enregistrer un commentaire