并发字典和不相交的键集
有 3 个线程。它们每个都使用自己的一组字典键来工作(读取、写入)。所以不同线程的键是互斥的。还有多个线程只读取数据。
这两种方法中哪一种在速度方面更高效:
- 创建一个字典(ConcurrentDictionary 类型)
- 为这 3 个线程中的每一个线程创建一个单独的字典(ConcurrentDictionary 类型)。
乍一看,第二种方法更有效,因为没有作者争用。这里有什么陷阱吗?如果两种方法之间的差异微不足道,那么我会选择第一种方法。
There are 3 threads. Each of them works (reads, writes) with its own set of dictionary keys. So keys are mutually exclusive for different threads. There are also multiple threads which only read data.
Which of these two approaches is more efficient in terms of speed:
- Create a single dictionary (of type ConcurrentDictionary)
- Create a separate dictionary (of type ConcurrentDictionary) for each of those 3 threads.
At first glance, the 2nd approach is more efficient since there are no writer contentions. Are there any pitfalls here? If difference between two approaches is insignificant then I would go with the 1st approach.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第二种方法更有效。无论如何,共享状态都不是一个好主意。
The 2nd approach is more efficient. In any case it's not a good idea to have a shared state.