我是靠谱客的博主 开心鸭子,最近开发中收集的这篇文章主要介绍session原理概述_数组工作原理概述 数组工作原理概述 (An overview of how arrays work),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

session原理概述

by Edison Yap

由叶冠文

数组工作原理概述 (An overview of how arrays work)

In computer science, there is the concept of a linear data structure, which means that the data are structured in a linear way in which order matters. There are Arrays and Linked Lists, but today I’ll be talking mostly about arrays, and a little about linked lists.

在计算机科学中,存在线性数据结构的概念,这意味着数据以线性方式构建,其中顺序很重要 。 有ArraysLinked Lists ,但是今天我将主要讨论数组,以及一些有关链表的内容。

Most object-orientated languages come with Arrays, whereas most functional languages come with Linked Lists (see why in another one of my articles, mentioned at the bottom of this article).

大多数面向对象的语言来使用数组 ,而大多数˚Functional语言来与链接列表 (见为什么在我的文章中的另一个,在本文的底部所提及的)。

There’s a good reason for this differentiation which we will dive into later. For now let’s just have a quick look at the differences between the two data structures. To do this, we’ll need to take a trip down memory lane.

这种差异化有充分的理由,我们将在后面进行探讨。 现在,让我们快速看一下这两个数据结构之间的区别。 为此,我们需要沿着存储通道走一趟。

倒带时间 (Rewind Time)

Objects and functions and everything that we know about computers, are fundamentally stored in bits and bytes in the computer.

对象和函数以及我们所了解的有关计算机的所有内容基本上都存储在计算机中的位和字节中。

In languages like Java and C, you have to explicitly declare the size of an array beforehand.

在Java和C之类的语言中,您必须事先明确声明数组的大小。

Hold up, but Ruby does not do that?

忍受,但是Ruby不这样做吗?

In Ruby, we use Array for our linear data structures. We can add seemingly infinite things into a Ruby array and it would not matter as far as we are concerned.

在Ruby中,我们将Array用于线性数据结构。 我们可以将看似无限的东西添加到Ruby数组中,就我们而言,这无关紧要。

That’s great isn’t it?! That means arrays are infinitely large right? And that Ruby is the superior language? Lucky us!

太好了吗? 那意味着数组是无限大的吧? 那Ruby是高级语言吗? 幸运的我们!

But not so fast. *pops your bubble*

但是没有那么快。 *弹出您的泡泡*

There are no infinite size arrays; what you see in Ruby is what we call a Dynamic Array, and it comes with a cost.

没有无限大小的数组。 您在Ruby中看到的就是所谓的Dynamic Array ,它带有成本。

To understand what dynamic arrays are, let’s first take a look at how arrays are represented in memory. Since MRI Ruby (Matz’ Ruby Interpreter) compiles down to C code, we’ll look at how arrays are represented in C.

为了了解什么是动态数组,让我们首先看一下数组在内存中的表示方式。 由于MRI Ruby(Matz的Ruby解释器)编译为C代码,因此我们将研究数组如何用C表示。

C-ing相信 (C-ing is believing)

We’ll dive into a little bit of C code to help us C a little better… :)

我们将深入研究一些C代码,以帮助我们更好地使用C…:)

In lower level languages like C, you have to deal with pointers and memory allocation yourself. Even if you have not dealt with C before (disclaimer — neither have I), you may have C-een one of the most (in)famous examples below:

在像C这样的低级语言中,您必须自己处理指针和内存分配。 即使您以前从未处理过C(d 声明者,也没有I ),您都可能会得到C-een以下最著名的例子之一:

Let’s break down this bit of code:

让我们分解一下这段代码:

  • malloc doesn't have any magic meaning behind it, it literally stands for memory allocation

    malloc背后没有任何魔术的含义,它实际上代表memory allocation

  • malloc returns a pointer

    malloc返回一个指针

  • malloc takes in an argument, which is the size the memory you want the program to allocate for you.

    malloc接受一个参数,该参数是您希望程序为您分配的内存大小。

  • 100 * sizeof(int) tells the program that we want to store 100 integers, so allocate us 100 * the size of what each integer would occupy.

    100 * sizeof(int)告诉程序我们要存储100个整数,因此请为我们分配100 *每个整数将占用的大小。

  • ptr/pointer stores reference to the memory address.

    ptr / pointer存储对内存地址的引用。

蒂米存放行李! (Timmy stores luggage!)

If the above example did not really make sense, try this analogy. Think of memory allocation as a luggage concierge. It works like this:

如果上面的示例没有真正意义,请尝试这种类比。 将内存分配视为行李礼宾人员。 它是这样的:

  • Timmy goes to the counter, tells the concierge that he has 2 pieces of luggage, about this big, and that that he’d like to store them in the storeroom.

    蒂米(Timmy)去柜台,告诉门房他有2件行李,大约这么大,他想把它们存放在储藏室里。

  • The concierge takes a look at the store room and go “Yes, we’ve got some room at the designated B4 area and will allocate that space to store your luggage".

    礼宾人员看了一眼储藏室,然后说:“是的,我们在指定的B4区域有一些空间,并将分配该空间来存储您的行李”。

  • They hand Timmy a pick-up card with the designated area on it, B4 in our case.

    他们递给蒂米一张提货卡 ,上面有指定的区域,在本例中为B4

  • Timmy is happy, goes around doing whatever, and when he wants to pick-up his luggage, he goes back to the counter and shows them his pick-up card. “Have you C-een my luggage?

    蒂米很高兴,可以做任何事情,当他想提起行李时,他回到柜台向他们出示了他的提货卡 。 “ 你有行李吗?

In our example, Timmy’s luggage is the data, the pick-up card is the pointer (it states where Timmy’s bag is stored). The place that the concierge is storing Timmy’s luggage is the memory block, and the counter is the program.

在我们的示例中,蒂米的行李是数据提货卡是指针 (指出蒂米的行李存放的位置)。 礼宾存放蒂米行李的地方是存储块 ,柜台是程序

By showing the counter (the program) Timmy’s card (pointer/memory address), Timmy can retrieve his luggage (data). Bonus? Because they know exactly where Timmy’s bag is stored — B4, this means that they can retrieve all Timmy's luggage relatively quickly!

通过显示柜台( 程序 )蒂米的卡( 指针/内存地址 ),蒂米可以取回他的行李( 数据 )。 奖金? 因为他们知道确切位置蒂米的包存储- B4 ,这意味着他们可以获取所有蒂米的行李比较快!

Also, ever wondered why you access elements in array with index, like so?

另外,有没有想过为什么要像这样访问带有index的数组中的元素?

This is because the array holds the references to the memory block, and the index is telling it the offset.

这是因为数组保存对内存块的引用,而索引告诉它offset

An analogy for that is if I ask you to look for Timmy in a queue of 20 people, you would logically have to ask each of them if they were Timmy. But, if I told you Timmy is the 6th (index) from the first person (your original pointer), you know exactly where to look.

打个比方,如果我要求您在20个人的队列中寻找Timmy,那么从逻辑上讲,您必须问他们每个人是否都是Timmy。 但是,如果我告诉您Timmy是第一人称( 您的原始指针 )的第6个( 索引 ),那么您确切地知道在哪里看。

Retrieving elements in arrays is fast exactly because of this — the program doesn’t have to look through all 100 elements to find what you’re looking for. If you’ve got the index, it just has to add the offset to the original memory address, and the droid that you were looking for will be right there!

正是由于这个原因,检索数组中的元素很快—程序不必遍历所有100个元素来查找所需的内容。 如果您有索引,则只需将偏移量添加到原始内存地址,然后您正在寻找的机器人将就在那!

那么什么是动态数组? (What are dynamic arrays then?)

So I’ve told you a little about how arrays are represented in memory, but now it’s time to talk about some cons.

因此,我已经告诉了您一些有关数组在内存中的表示方式的信息,但是现在该讨论一些缺点了。

Remember how you have to explicitly declare the amount of memory you need? This means that the array will find a spot that will fit exactly your size. There’s no guarantee that it will be able to fit more than what you have (because the memory block right behind it might be occupied).

还记得您必须如何显式声明所需的内存量吗? 这意味着阵列将找到一个完全适合您的大小的点。 无法保证它可以容纳的容量超出了您所拥有的容量(因为它后面的内存块可能已被占用)。

Back to our luggage analogy: think of it as if Timmy was to store 2 pieces of luggage, and B4can store exactly 2 pieces of luggage, so they allocate that to Timmy. Now for some reason Timmy wants to store another piece of luggage, but B4 can't store 3 pieces, only 2, so what do they do?

回到我们的行李类比:想想蒂米要存储2件行李,而B4可以恰好存储2件行李,所以他们将它们分配给蒂米。 现在,由于某种原因,邓肯希望存储另一件行李,但B4不能存储3个,只有2个,所以他们在做什么?

They take all of his existing luggage, move it into a new spot that can fit more than 3 pieces, and then store all of them together.

他们拿走了他所有的现有行李,将其搬到可以容纳3件以上的地方,然后将所有行李存放在一起。

That is an expensive operation but it’s exactly how memory works too!

这是一项昂贵的操作,但也正是内存的工作方式!

In Ruby, you don’t have to declare a specific size before hand, but that’s because Ruby handles it for you automagically through dynamic arrays.

在Ruby中,您不必事先声明特定的大小,但这是因为Ruby通过动态数组自动为您处理它。

What a dynamic array does is that if the array is nearing its full capacity, it’ll automatically declare a new, bigger array and move all existing elements into it, and the old array is then garbage collected. How much bigger? The growth factor is usually 2; double the size of the current array.

动态数组的作用是,如果该数组已接近其全部容量,它将自动声明一个更大的新数组并将所有现有元素移入其中,然后对旧数组进行垃圾回收。 大多少? 生长因子通常为 2; 将当前数组的大小加倍。

In fact, don’t take my word for it.

实际上,不要相信我

Ruby has an ObjectSpace module that allows us to interact with current objects living in memory. We can use this module to take a peek at the memory usage of our dynamic array — sounds exactly like what we want!

Ruby有一个ObjectSpace模块 ,该模块允许我们与内存中的当前对象进行交互。 我们可以使用该模块来查看动态数组的内存使用情况-听起来完全像我们想要的!

I have written a small Ruby script that calculates the growth factor of the dynamic array. Feel free to take a look at it here, and if you do, you can see that Ruby arrays have a 1.5x growth factor (that is, they make an array that’s 50% bigger on every copy).

我写了一个小的Ruby脚本来计算动态数组的增长因子。 随意在这里查看它,如果您这样做了,您会发现Ruby数组的增长因子是1.5倍(也就是说,它们使每个副本的数组大50%)。

我知道什么是数组,什么是链表? (I know what arrays are, what are linked lists?)

Keep in mind that although arrays and linked lists are both considered linear data structures, they have one big difference between them.

请记住,尽管数组和链表都被认为是线性数据结构,但是它们之间有很大的不同。

Elements in an array are stored literally right next to each other in memory (so we can have index for fast lookups). But nodes in linked lists have no such restriction (which is why there’s no index lookup for linked lists) — each and every item can be stored anywhere on the memory block.

数组中的元素实际上彼此相邻地存储在内存中(因此我们可以使用索引进行快速查找)。 但是链表中的节点没有这种限制(这就是为什么链表没有索引查找的原因)-每个项目都可以存储在内存块中的任何位置。

It’s almost as if Timmy’s trying to store 5 pieces of luggage, and the concierge does not have a space and decides to leave them all over the place. Sounds unorganized?

就像蒂米(Timmy)试图储存5件行李一样,门房没有空间,决定将它们放到整个地方。 听起来杂乱无章?

Also if they are stored in different places, how do you know which bags are Timmy’s? Hint: Just keep track of the next node/bag! In our case, the concierge keeps them separately but with a tag on each of them that points to the next bag.

另外,如果将它们存放在不同的地方,您怎么知道蒂米的那些袋子? 提示:只需跟踪下一个节点/包! 在我们的情况下,礼宾人员将它们分开保存,但每个标签上都有一个指向下一个行李袋的标签。

A node in a linked list consists of two parts — the data part and a pointer to the next node. This is how they’re able to maintain the linear part of it -- they still have the concept of order, they just don't have to be stored in order literally!

链表中的节点由两部分组成-数据部分和指向下一个节点的指针。 这就是他们保持linear部分的方式-他们仍然拥有顺序的概念,只是不必按字面顺序存储它们!

node = [ data | pointer ]

node = [ data | pointer ]

For example, given the following example stored in memory:

例如,给定以下示例存储在内存中:

[C | D] [A | B] [B | C] [D | nil]

[C | D] [A | B] [B | C] [D | nil]

This bits look like it’s out of order — but if I had told you that the first element is A, you would be able to tell me the exact order of the list:

这个位看起来好像是乱序的-但如果我告诉你第一个元素是A ,则可以告诉我列表的确切顺序:

list = [A -> B -> C -> D -> nil]

list = [A -> B -> C -> D->无]

There’s a lot of interesting things that you can do with linked lists which I’m not going to dive into here (also a lot on Big O that I didn’t talk about). But there are already a ton of good articles out there on data structures. If you made it here, I suggest you to read from Ali’s blogpost here.

使用链接列表可以做很多有趣的事情,在这里我将不做介绍(关于Big O,我也没有谈论过很多)。 但是已经有大量关于数据结构的好文章。 如果您在这里做到了,建议您阅读Ali 在这里的博客文章。

thank u, next: an introduction to linked listsIn this post, we are going to be talking about the linked list data structure in the language of "thank u, next" by…dev.to

谢谢,下一篇:链表的介绍 在本文中,我们将以... dev.to 的“谢谢,下一个”的语言讨论链表数据结构。

You can also read more about List/Cons on Wiki here.

您也可以在Wiki上了解有关List / Cons的更多信息 。

结语 (Closing Note)

I initially wrote this article for a slightly different topic — [ Elixir | Why Linked Lists?], but found it took way too long to explain how arrays work before I was able to explain explore why Elixir uses linked lists. So I have separated them into two articles.

我最初写这篇文章的目的是一个略有不同的主题- [Elixir | 为什么使用链表?] ,但是发现在解释为什么Elixir使用链表之前花了很长时间来解释数组的工作原理。 因此,我将它们分为两篇文章。

In that article, I talk about why functional languages use linked lists as their linear data structure. Do check it out!

在那篇文章中,我讨论了为什么功能语言将链接列表用作其线性数据结构。 请检查一下!

[ Elixir | Why Linked Lists? ]I've always thought data structures are cool, but you know what's cooler? Seeing them in the wild! While going through…dev.to

[长生不老药| 为什么要链接列表? ] 我一直认为数据结构很酷,但是您知道什么更酷吗? 在野外看到它们! 在经历… dev.to

资料来源 (Sources)

  1. https://medium.com/@rebo_dood/ruby-has-a-memory-problem-part-1-7887bbacc579 — This is where I found out about additional ObjectSpace methods by requiring it

    https://medium.com/@rebo_dood/ruby-has-a-memory-problem-part-1-7887bbacc579 —在这里,我通过要求找到了其他ObjectSpace方法

Originally posted on dev.to

最初发布于dev.to

翻译自: https://www.freecodecamp.org/news/how-arrays-work-the-way-arrays-work-a775bfee519e/

session原理概述

最后

以上就是开心鸭子为你收集整理的session原理概述_数组工作原理概述 数组工作原理概述 (An overview of how arrays work)的全部内容,希望文章能够帮你解决session原理概述_数组工作原理概述 数组工作原理概述 (An overview of how arrays work)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(59)

评论列表共有 0 条评论

立即
投稿
返回
顶部