简体   繁体   English

计算由fx =(x,x)完成的工作

[英]Calculating work done by f x = (x,x)

Let's say I have this function: (Haskell syntax) 假设我有这个函数:( Haskell语法)

f x = (x,x)

What is the work (amount of calculation) performed by the function? 该功能执行的工作(计算量)是多少?

At first I thought it was obviously constant, but what if the type of x is not finite, meaning, x can take an arbitrary amount of memory? 起初我认为它显然是不变的,但是如果x的类型不是有限的,那意味着x可以占用任意数量的内存? One would have to take into account the work done by copying x as well, right? 人们必须考虑到复制x所做的工作,对吗?

This led me to believe that the work done by the function is actually linear in the size of the input. 这让我相信函数完成的工作实际上是输入大小的线性。

This isn't homework for itself, but came up when I had to define the work done by the function: 这不是本身的功课,但是在我必须定义函数完成的工作时出现了:

f x = [x]

Which has a similar issue, I believe. 我相信哪个有类似的问题。

Very informally, the work done depends on your language's operational semantics. 非正式地,完成的工作取决于您的语言的操作语义。 Haskell, well, it's lazy, so you pay only constant factors to: Haskell,嗯,它很懒,所以你只需支付常数因素:

  • push pointers to x on the stack 将指针推送到堆栈上的x
  • allocate a heap cell for (,) (,)分配一个堆单元
  • apply (,) to its arguments (,)应用于其参数
  • return a pointer to the heap cell 返回指向堆单元格的指针

Done. 完成。 O(1) work, performed when the caller looks at the result of f . O(1)工作,当调用者查看f的结果时执行。

Now, you will trigger further evaluation if you look inside the (,) -- and that work is dependent on the work to evaluate x itself. 现在,如果您查看(,)内部,您将触发进一步的评估 - 并且该工作取决于评估x本身的工作。 Since in Haskell the references to x are shared, you evaluate it only once. 因为在Haskell中对x的引用是共享的,所以只评估它一次。

So the work in Haskell is O(work of x) if you fully evaluate the result. 因此,如果您完全评估结果,Haskell中的工作是O(x的工作) Your function f only adds constant factors. 你的函数f只增加了常数因子。

Chris Okasaki has a wonderful method of determining the work charged to function call when some (or total) laziness is introduced. 当一些(或完全)懒惰被引入时,Chris Okasaki有一种很好的方法来确定函数调用的工作量。 I believe it is in his paper on Purely Functional Data Structures. 我相信这是关于纯功能数据结构的论文。 I know it is in the book version -- I read that part of the book last month. 我知道这是书的版本 - 我上个月读了这本书的那一部分。 Basically you charge a constant factor for the promise/thunk created, charge nothing for evaluating any passed in promises/thunks (assume they've already been forced / are in normal form [not just WHNF]). 基本上你为一个承诺/ thunk创建了一个恒定的因子,没有任何费用来评估任何传递的promises / thunk(假设它们已经被强制/处于正常形式[不仅仅是WHNF])。 That's an underestimate. 这是低估的。 If you want an overestimate charge also the cost of forcing / converting to normal form each promise / thunk created by the function. 如果你想要高估费用,强制/转换为正常的成本也会形成由函数创建的每个promise / thunk。 At least, that's how I remember it in my extremely tired state. 至少,这就是我在极度疲惫的状态下记忆的方式。

Look it up in Okasaki: http://www.westpoint.edu/eecs/SitePages/Chris%20Okasaki.aspx#thesis -- I swear the thesis used be be downloadable. 在Okasaki查找: http//www.westpoint.edu/eecs/SitePages/Chris%20Okasaki.aspx#thesis - 我发誓所使用的论文可以下载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM