英文:
Understanding Rob Pike's book incineration example
问题
在Rob Pike的2012年演讲《并发不等于并行(它更好)》中,他举了一个例子,描述了一群地鼠一起工作,将一堆书烧毁。
在第15张幻灯片(视频6:58)中,有三只地鼠在工作:
- 一只地鼠将书从一堆书中装入手推车。
- 一只地鼠将装满书的手推车移动到焚烧炉(然后将空车推回来)。
- 一只地鼠将书从手推车中移入焚烧炉。
在第16张幻灯片中,有四只地鼠一起解决同样的问题:
- 一只地鼠将书从一堆书中装入手推车(与之前相同)。
- 一只地鼠将装满书的手推车移动到焚烧炉。
- 一只地鼠将书从手推车中移入焚烧炉(与之前相同)。
- 一只地鼠将空车推回到书堆。
Pike表示,这样做可以完成更多的工作,而且速度更快。为什么?看起来工作量是一样的,只是由更多的地鼠共同完成。
英文:
In Rob Pike's talk 2012 talk, Concurrency is not Parallelism (it's better), he has a running examples of a group of gophers working together to incinerate a pile of books.
On slide 15 (video 6:58), there are three gophers at work:
- One loading books from a pile into a cart.
- One moving the filled cart to the incinerator (and later returning the empty cart).
- One moving the books from the cart into the incinerator.
On slide 16, four gophers are working together on the same problem:
- One loading books from a pile into a cart (as before).
- One moving the filled cart to the incinerator.
- One moving the books from the cart into the incinerator (as before).
- One moving the empty cart back to the pile.
Pike says that this performs more work but will run faster. Why? It seems like the same amount of work, shared by more gophers.
答案1
得分: 3
这个技巧在于,并不只有一个推车,而是有多个推车。一个推车正在被填充,一个推车正在被移动到焚化炉,一个推车正在被倒空到焚化炉,还有一个推车正在从焚化炉移回来。这意味着每只地鼠应该(只要事情正常运作且每个任务的完成时间相对相似)始终保持忙碌。
英文:
The trick is that there isn't just one cart, there are multiple carts. One being filled, one being moved to the incinerator, on being emptied into the incinerator, and one being moved back from the incinerator. This means that each gopher should (as long as things are working reasonably and the completion time of each task is relatively similar) always be busy.
答案2
得分: 1
假设背负着装满书的推车的地鼠在从推车上卸下书籍后需要进行一些清理或沟通工作。现在,他可以在另一个地鼠将空推车带回来的时候进行这些工作。因此,装载书籍的地鼠不需要等待那么长时间才能等到推车回来。
英文:
The assumption is that the gopher carrying the loaded cart has to perform some kind of cleanup or communication after removing the books from the cart. Now he can do that while the other gopher carries the empty cart back. Therefore, the loading gopher doesn't have to wait as long for the cart to come back.
答案3
得分: 1
考虑一个推车的往返行程。
在第一种情况下:
- 一个人将装满货物的推车移动到焚化炉(然后将空推车返回到堆放处)。
即:
- Gopher 1:将装满货物的推车移动到焚化炉。
- Gopher 1:将空推车带回到堆放处。
在第二种情况下:
- 一个人将装满货物的推车移动到焚化炉。
- 一个人将空推车带回到堆放处。
即:
- Gopher 1:将装满货物的推车移动到焚化炉。
- Gopher 1:不带推车返回到堆放处。
- Gopher 2:将空推车带回到堆放处。
- Gopher 2:不带推车返回到焚化炉。
虽然我们增加了额外的工作,但由于分工的原因,它可能运行得更快。
关于劳动分工的问题:
以制钉业为例,这是一个非常微不足道的制造业,但劳动分工在其中被经常提及。一个没有接受过这项业务教育(劳动分工使其成为一个独立的行业)或不熟悉其中所使用的机械设备(这些机械设备的发明可能是由于劳动分工的原因)的工人,也许一天最多只能制造一个钉子,而且肯定不能制造二十个。但在现在进行这项业务的方式中,不仅整个工作是一个独特的行业,而且它被分成了许多分支,其中大部分也是独特的行业。一个人拉出钢丝;另一个将其拉直;第三个将其切断;第四个将其尖化;第五个将其顶部磨平以接收头部;制作头部需要两到三个不同的操作;安装头部是一项特殊的工作;将钉子漂白是另一项工作;将它们放入纸张中甚至是一个独立的行业;制造一根钉子的重要工作以这种方式分为大约十八个不同的操作,在某些工厂中,所有这些操作都由不同的工人完成,尽管在其他工厂中,同一个人有时会执行其中的两到三个操作。我曾经见过一个这样的小型制造厂,只雇佣了十个人,因此其中一些人会执行两到三个不同的操作。但尽管他们非常贫穷,因此在必要的机械设备方面条件不佳,但当他们努力工作时,他们一天之内可以制造约十二磅的钉子。一磅中有四千多根中等大小的钉子。因此,这十个人一天之内可以制造四万八千根钉子。因此,每个人制造四千八百根钉子,可以被认为是每天制造的数量。但是,如果他们都是独立地工作,没有任何一个人接受过这种特殊业务的教育,并且没有一个人能够每天制造二十个钉子,也许甚至不能制造一个钉子;也就是说,肯定不能达到他们目前能够完成的工作的二百四十分之一,也许甚至不能达到四千八百分之一。
英文:
Consider a round trip for a cart load.
In the first case we have:
- One moving the filled cart to the incinerator (and later returning the empty cart).
That is:
-
Gopher 1: Move the filled cart to the incinerator.
-
Gopher 1: Move the empty cart back to the pile.
In the second case we have:
-
One moving the filled cart to the incinerator.
-
One moving the empty cart back to the pile.
That is:
-
Gopher 1: Move the filled cart to the incinerator.
-
Gopher 1: Walk back to the pile with no cart.
-
Gopher 2: Move the empty cart back to the pile.
-
Gopher 2: Walk back to the incinerator with no cart.
Although we have extra work, it may run faster for several reasons, for example, division of labor.
> Of the Division of Labour
>
> To take an example, therefore, from a very trifling manufacture, but
> one in which the division of labour has been very often taken notice
> of, the trade of a pin-maker: a workman not educated to this business
> (which the division of labour has rendered a distinct trade), nor
> acquainted with the use of the machinery employed in it (to the
> invention of which the same division of labour has probably given
> occasion), could scarce, perhaps, with his utmost industry, make one
> pin in a day, and certainly could not make twenty. But in the way in
> which this business is now carried on, not only the whole work is a
> peculiar trade, but it is divided into a number of branches, of which
> the greater part are likewise peculiar trades. One man draws out the
> wire; another straights it; a third cuts it; a fourth points it; a
> fifth grinds it at the top for receiving the head; to make the head
> requires two or three distinct operations; to put it on is a peculiar
> business; to whiten the pins is another; it is even a trade by itself
> to put them into the paper; and the important business of making a pin
> is, in this manner, divided into about eighteen distinct operations,
> which, in some manufactories, are all performed by distinct hands,
> though in others the same man will sometimes perform two or three of
> them. I have seen a small manufactory of this kind, where ten men only
> were employed, and where some of them consequently performed two or
> three distinct operations. But though they were very poor, and
> therefore but indifferently accommodated with the necessary machinery,
> they could, when they exerted themselves, make among them about twelve
> pounds of pins in a day. There are in a pound upwards of four thousand
> pins of a middling size. Those ten persons, therefore, could make
> among them upwards of forty-eight thousand pins in a day. Each person,
> therefore, making a tenth part of forty-eight thousand pins, might be
> considered as making four thousand eight hundred pins in a day. But if
> they had all wrought separately and independently, and without any of
> them having been educated to this peculiar business, they certainly
> could not each of them have made twenty, perhaps not one pin in a day;
> that is, certainly, not the two hundred and fortieth, perhaps not the
> four thousand eight hundredth, part of what they are at present
> capable of performing, in consequence of a proper division and
> combination of their different operations.
>
> An Inquiry into the Nature and Causes of the Wealth of Nations, Adam
> Smith, 1776.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论