作为数据容器,vector和list之间的主要区别是什么 [英] As a data container, what are the main differences between vector and list

查看:82
本文介绍了作为数据容器,vector和list之间的主要区别是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我们需要一个数字列表,有两个定义:

Say we need a list of numbers, there are two definitions:

(def vector1 [1 2 3])
(def list2 '(1 2 3))

那么主要区别是什么?

推荐答案

[1 2 3] 向量,而'(1 2 3)列表。这两种数据结构具有不同的性能特征。

The [1 2 3] is a vector, whereas '(1 2 3) is a list. There are different performance characteristics of these two data structures.

向量可以快速,索引地随机访问其元素(v 34)返回向量<$ c $的元素c> v 在 O(1)时间的索引 34 处。另一方面,修改向量通常更昂贵。

Vectors provide quick, indexed random access to its elements (v 34) returns element of vector v at index 34 in O(1) time. On the other hand it is generally more expensive to modify vectors.

列表易于在头部和/或尾部进行修改(取决于实现方式),但可以线性访问元素:(nth(列表1 2 3 4 5)3)需要顺序扫描列表。

Lists are easy to modify at head and/or tail (depending on implementation), but provide linear access to elements: (nth (list 1 2 3 4 5) 3) requires sequential scan of the list.

有关性能折衷的更多信息,可以在Google上查看矢量与列表性能或类似的信息。

For more information on the performance tradeoffs you can google "vector vs. list performance" or something similar.

[关注]

好吧,让我们更详细些。首先,向量列表是不特定于Clojure的概念。与地图队列等一起,它们是数据集合的抽象类型。根据这些抽象定义对数据进行运算的算法。 向量列表是由我上面简要描述的行为(即某些东西向量)定义的如果具有大小,则可以按固定的时间访问它的元素并进行索引等。)

Ok, lets get into some more detail. First of all vectors and lists are concepts that are not specific to Clojure. Along with maps, queues, etc., they are abstract types of collections of data. Algorithms operating on data are defined in terms of those abstractions. Vectors and lists are defined by the behavior that I briefly described above (i.e. something is a vector if it has size, you can access its elements by and index in constant time etc.).

Clojure和其他任何语言一样,都希望满足这些要求提供以这种方式调用的数据结构时的期望。如果您查看 <$ c $的基本实现在 vector 中的c> nth ,您会看到对 arrayFor 方法,复杂度为 O(log 32 N),并且在Java数组中查找为 O(1)

Clojure, as any other language, wants to fulfill those expectations when providing data structures that are called this way. If you'll look at the basic implementation of nth in vector, you'll see a call to arrayFor method which has the complexity of O(log32N) and a lookup in Java array which is O(1).

为什么我们可以说(v 34)实际上是 O(1)?因为Java int log 32 的最大值约为 7 。这意味着对向量的随机访问实际上是恒定时间。

Why can we say that (v 34) is in fact O(1)? Because the maximum value of log32 for a Java int is around 7. This means that random access to a vector is de facto constant time.

总而言之,个向量之间的主要区别列表实际上是 的性能特征。此外,正如Jeremy Heiler指出的那样,在Clojure中,行为上存在逻辑上的差异,即在使用 conj

In summary, the main difference between vectors and lists really is the performance characteristics. Additionally, as Jeremy Heiler points out, in Clojure there are logical differences in behaviour, i.e. with respect to growing the collection with conj.

这篇关于作为数据容器,vector和list之间的主要区别是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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