F#:字典,哈希表和地图之间的区别 [英] F#: Difference between Dictionary, Hashtable and Map

查看:123
本文介绍了F#:字典,哈希表和地图之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是.NET编程的新手.抱歉,以前是否有人问过这个问题.

I am new to .NET programming. Sorry if this question has been asked before.

我目前正在学习F#.字典,哈希表和地图之间有什么区别?我应该何时使用它们?

I am currently learning F#. What are the differences between Dictionary, Hashtable and Map? When should I use each?

我还有另一个标题中未提及的问题.什么时候应该使用Async.RunSynchronously?在我看来,这是自相矛盾的,所以我确定我缺少一些东西.

I also have another question that is not mentioned in the title. When should I use Async.RunSynchronously? It seems rather self-contradictory to me, so I am sure that I am missing something.

推荐答案

Dictionary,Hashtable和Map之间的选择取决于用例.但是,您应该了解每个特征.这不是一个详尽的列表,而只是您可能要从中开始的一些关键区别:

The choice between Dictionary, Hashtable and Map depends on the uses cases. You should however know the characteristics of each. This is not an exhaustive list but just some key differences you might want to start from :

  • 哈希表表示基于键的哈希码组织的键/值对的集合.这是来自.NET BCL的可变集合
  • Dictionary<> :这是哈希表的通用实现.也是来自.NET BCL的可变集合
  • 地图.这是F#不可变的类型.它是基于AVL树实现的,该树是一个完全不同的数据结构,具有不同的性能特征和用例.
  • Hashtable Represents a collection of key/value pairs that are organized based on the hash code of the key. This is a mutable collection from .NET BCL
  • Dictionary<> this is a generic implementation of the hashtable. Also a mutable collection from .NET BCL
  • Map This is the F# immutable type. It is implemented based on AVL trees which is an entirely different data structure with different performance characteristics and use cases.

如果您要进行多次写操作,则哈希表集合的填充率性能要比AVL树好得多.

If you are doing many writes, Hash tables collections have significantly better fill rate performance than AVL trees.

使用Dictionary的键从Dictionary检索值非常快,接近 O(1),因为Dictionary类是作为哈希表实现的.

Retrieving a value from a Dictionary by using its key is very fast, close to O(1), because the Dictionary class is implemented as a hash table.

F#映射被实现为不可变的AVL树,这是一种有效的数据结构,可形成自平衡的二叉树. AVL树以其效率着称,可以在 O(log n)时间内搜索,插入和删除树中的元素,其中n是树中的元素数.

F# maps are implemented as immutable AVL trees, an efficient data structure which forms a self-balancing binary tree. AVL trees are well-known for their efficiency, in which they can search, insert, and delete elements in the tree in O(log n) time, where n is the number of elements in the tree.

对于map用例来说,如果您有一组静态数据(例如,在应用程序启动时加载的配置数据),则需要经常按键查找,那么Map是一个很好的选择任何情况下,它的不变性确保了静态数据不会被错误地修改,并且对性能几乎没有影响,因为您无需在初始化后对其进行突变.

As for the map uses case, if you’ve got a set of sta­tic data (such as con­fig­u­ra­tion data that’s loaded when your appli­ca­tion starts up) you need to look up by key fre­quently, a Map is as good a choice as any, its immutabil­ity in this case ensures that the sta­tic data can­not be mod­i­fied by mis­take and has lit­tle impact to per­for­mance as you never need to mutate it once initialized.

Async.RunSynchronously运行提供的异步计算并等待其结果.例如,您可以在F#交互式窗口中使用它来测试异步工作流.

Async.RunSynchronously runs the provided asynchronous computation and awaits its result. You can use it in F# interactive window for example to test your asynchronous workflows.

这篇关于F#:字典,哈希表和地图之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆