更改

跳到导航 跳到搜索
删除1,119字节 、 2020年12月27日 (日) 23:28
第1,032行: 第1,032行:  
===性能问题===
 
===性能问题===
   −
In languages (such as [[C (programming language)|C]] and [[Java (programming language)|Java]]) that favor iterative looping constructs, there is usually significant time and space cost associated with recursive programs, due to the overhead required to manage the stack and the relative slowness of function calls; in [[functional languages]], a function call (particularly a [[tail call]]) is typically a very fast operation, and the difference is usually less noticeable.
+
 
    
在偏重于迭代循环结构的语言(如C语言和Java)中,由于管理栈所需的开销,以及函数调用的相对缓慢,递归程序通常会有显著的时间和空间成本;在函数式语言中,函数调用(尤其是尾部调用)通常是一个非常快的操作,这种差异通常不太明显。
 
在偏重于迭代循环结构的语言(如C语言和Java)中,由于管理栈所需的开销,以及函数调用的相对缓慢,递归程序通常会有显著的时间和空间成本;在函数式语言中,函数调用(尤其是尾部调用)通常是一个非常快的操作,这种差异通常不太明显。
   −
As a concrete example, the difference in performance between recursive and iterative implementations of the "factorial" example above depends highly on the [[compiler]] used. In languages where looping constructs are preferred, the iterative version may be as much as several [[order of magnitude|orders of magnitude]] faster than the recursive one. In functional languages, the overall time difference of the two implementations may be negligible; in fact, the cost of multiplying the larger numbers first rather than the smaller numbers (which the iterative version given here happens to do) may overwhelm any time saved by choosing iteration.
     −
作为一个具体的例子,上述 "阶乘 "例子的递归和迭代实现之间的性能差异很大程度上取决于所使用的编译器。在偏好循环结构的语言中,迭代版本可能比递归版本快几个数量级。在函数式语言中,两种实现的总体时间差异可能可以忽略不计;事实上,先乘大数而不是先乘小数的成本(这里给出的迭代版本就是这么做的)可能会超过选择迭代所节省得任何时间。
+
 
 +
作为一个具体的例子,上述 "阶乘"例子的递归和迭代实现之间的性能差异很大程度上取决于所使用的编译器。在偏好循环结构的语言中,迭代版本可能比递归版本快几个数量级。在函数式语言中,两种实现的总体时间差异可能可以忽略不计;事实上,先乘大数而不是先乘小数的成本(这里给出的迭代版本就是这么做的)可能会超过选择迭代所节省得任何时间。
    
===栈空间===
 
===栈空间===

导航菜单