示例程序 [英] example program

查看:71
本文介绍了示例程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


可以给任何一个示例程序,其中递归版本比迭代版本更快吗?
$ div $ =

Hi all,

can any one give a example program where recursive version is faster
than iterative version ?

推荐答案

aa*****@gmail.com 写道:

大家好,


可以任何一个给出一个示例程序,其中递归版本更快

比迭代版?
Hi all,

can any one give a example program where recursive version is faster
than iterative version ?



对于琐碎的程序,可能有也可能没有例子递归是
更快。


但是递归是很多种编程的自然选择,这可能是用迭代方法实现的痛苦。


特别是安排保存/恢复复杂的数据可能比使用递归更慢。但即使递归速度慢了很多,在实际应用中差异也会很小,同时保持代码更清晰。


-

Bartc

For trivial programs there may or may not be examples where recursion is
faster.

But recursion is a natural fit for many kinds of programming which would be
a pain to implement with iterative methods.

Especially with making arrangements to save/restore complex data which may
well end up slower than just using recursion. But even if recursion was
slower, the difference would be minimal in a real application, while keeping
the code much cleaner.

--
Bartc


Bartc说:
Bartc said:
aa ***** @ gmail.com 写道:

>大家好,

任何人都可以提供一个示例程序,其中递归版本比迭代版本更快吗?
>Hi all,

can any one give a example program where recursive version is faster
than iterative version ?



对于琐碎的程序,可能会或可能不会有递归更快的例子。
更快。


For trivial programs there may or may not be examples where recursion is
faster.



然而,并不缺少递归为/慢/的示例。

There is, however, no shortage of examples where recursion is /slower/.


但是递归对于许多类型的编程来说,它很自然地适合用迭代方法实现的痛苦。
But recursion is a natural fit for many kinds of programming which would
be a pain to implement with iterative methods.



对。我们不会为速度而努力,但为了清晰起见(它/更清楚)

- 即使这样,只有当速度损失的成本超过
$ b时$ b充分补偿了清晰度的增益。


举一个着名的例子,下面的代码:


无符号长阶乘(unsigned long n)

{

返回n< 2? 1:(n *阶乘(n - 1));

}


与其迭代版本相比效率非常低(更多的是,

与斯特林近似相比)。在生产环境中,它不适合
。但是在一个教学环境中,它可能被认为是一种合理的/说明/递归的方式,给定合适的方式。

在Real中不做这样的因子寿命"警告。


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件: -http:// WWW。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

Right. We don''t recurse for speed, but for clarity (where it /is/ clearer)
- and even then only if the cost in terms of speed loss is more than
adequately compensated by the gain in clarity.

To take a famous example, the following code:

unsigned long factorial(unsigned long n)
{
return n < 2 ? 1 : (n * factorial(n - 1));
}

is horribly inefficient compared to its iterative version (and more so,
compared to the Stirling Approximation). In a production environment, it
would be inappropriate. But in a teaching environment, it might well be
considered a reasonable way to /illustrate/ recursion, given suitable
"don''t do factorials this way in Real Life" caveats.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


2008年6月2日星期一03:29:17 -0700(PDT), aa ***** @ gmail.com 写道:
On Mon, 2 Jun 2008 03:29:17 -0700 (PDT), aa*****@gmail.com wrote:

>大家好,
任何人都可以给出一个示例程序,其中递归版本比迭代版本更快吗?
>Hi all,

can any one give a example program where recursive version is faster
than iterative version ?



主要问题:您需要两个版本的程序。你如何确认它们是真正的等价物,并且每个都是真的编码为

效率最高?


次要问题:关于哪个硬件?使用哪个操作系统?

使用哪个编译器?有什么选择?哪种衡量速度,CPU

时间或挂钟?


您是否得到了没有一般答案的提示?

删除电子邮件的del

Primary concern: You need two versions of the program. How do you
confirm they are truly equivalent and that each is really coded for
highest efficiency?

Secondary concerns: On which hardware? Using which operating system?
Using which compiler? With what options? Which measure of speed, CPU
time or wall clock?

Are you getting the hint that there is no general answer?
Remove del for email


这篇关于示例程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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