为什么em而不是px?(Why em instead of px?)
我听说你应该用em代替像素来定义样式表中的大小和距离。 所以问题是为什么在css中定义样式时应该使用em而不是px? 有没有一个很好的例子说明这一点?
I heard you should define sizes and distances in your stylesheet with em instead of in pixels. So the question is why should I use em instead of px when defining styles in css? Is there a good example that illustrates this?
原文:https://stackoverflow.com/questions/609517
最满意答案
递归通常要慢得多,因为所有的函数调用必须存储在堆栈中才能返回到调用者函数。 在许多情况下,必须分配和复制内存才能实现范围隔离。
一些优化,如尾呼叫优化 ,使递归更快,但并不总是可能,并且不会以所有语言实现。
使用递归的主要原因是
- 在许多情况下,当它模拟我们的问题的方法时,它更直观
- 某些数据结构(如树)更容易使用递归来进行探索(或者在任何情况下都需要堆栈)
当然每个递归都可以被建模为一种循环:这就是CPU最终会做的。 而递归本身,更直接的意思是将函数调用和范围放在堆栈中。 但是将递归算法更改为循环算法可能需要大量工作,并使您的代码不太可维护:对于每个优化,只有当某些分析或证据显示出必要时才应该尝试。
Recursion is usually much slower because all function calls must be stored in a stack to allow the return back to the caller functions. In many cases, memory has to be allocated and copied to implement scope isolation.
Some optimizations, like tail call optimization, make recursions faster but aren't always possible, and aren't implemented in all languages.
The main reasons to use recursion are
- that it's more intuitive in many cases when it mimics our approach of the problem
- that some data structures like trees are easier to explore using recursion (or would need stacks in any case)
Of course every recursion can be modeled as a kind of loop : that's what the CPU will ultimately do. And the recursion itself, more directly, means putting the function calls and scopes in a stack. But changing your recursive algorithm to a looping one might need a lot of work and make your code less maintainable : as for every optimization, it should only be attempted when some profiling or evidence showed it to be necessary.
相关问答
更多-
对的,这是可能的。 可以用迭代替换任何递归。 使用尾端递归非常容易。 你只需将其更改为循环。 如果它不是尾部,那么你需要将局部变量存储在堆栈中并从堆栈中推送和弹出(因为这是一个递归函数调用给你的)。 虽然它首先看起来像你有尾端递归,但你没有,因为它在循环中。 当你循环到外部循环时,你需要创建一个外部循环并推送局部变量(如worker)。 Yes it is possible. It is possible to replace any recursion with iteration. It is real ...
-
递归与迭代(recursion versus iteration)[2023-01-22]
递归通常要慢得多,因为所有的函数调用必须存储在堆栈中才能返回到调用者函数。 在许多情况下,必须分配和复制内存才能实现范围隔离。 一些优化,如尾呼叫优化 ,使递归更快,但并不总是可能,并且不会以所有语言实现。 使用递归的主要原因是 在许多情况下,当它模拟我们的问题的方法时,它更直观 某些数据结构(如树)更容易使用递归来进行探索(或者在任何情况下都需要堆栈) 当然每个递归都可以被建模为一种循环:这就是CPU最终会做的。 而递归本身,更直接的意思是将函数调用和范围放在堆栈中。 但是将递归算法更改为循环算法可能需要 ... -
迭代比递归更好,对吧? 不必要。 这个概念来自许多C语言,其中调用函数,递归或不递归,具有很大的开销,并为每个调用创建了一个新的堆栈。 对于许多语言,情况并非如此,递归与迭代版本同等或更高的性能。 这些天,甚至一些C编译器将一些递归结构重写为迭代版本,或者重新使用堆栈帧进行尾递归调用。 Iteration is more performant than recursion, right? Not necessarily. This conception comes from many C-like lang ...
-
递归或迭代?(Recursion or Iteration?)[2021-01-07]
递归可能更昂贵,这取决于递归函数是否递归递归 (最后一行是递归调用)。 尾递归应由编译器识别并优化为其迭代对应方(同时保持您的代码中简洁明了的实现)。 我会以最有意义的方式编写算法,对于在几个月或几年内必须维护代码的穷人(无论是自己还是其他人)来说最为明确。 如果遇到性能问题,然后配置您的代码,然后再通过转移到迭代实现来查找优化。 您可能需要查看记忆和动态编程 。 It is possible that recursion will be more expensive, depending on if th ... -
从递归到迭代的方式(Way to go from recursion to iteration)[2022-12-23]
通常,我通过将通常传递给递归函数的参数推送到堆栈上,通过迭代算法来替换递归算法。 实际上,您正在用自己的方法替换程序堆栈。 Stack -
同时递归和迭代(Simultaneous recursion and iteration)[2022-03-14]
(编辑:OP在写完这个答案之后声明语言是matlab。我的代码示例将保持c风格,因为我不知道matlab。) 如果不知道它是哪种语言,这很难做到。你的函数应该带有两个参数:矩阵和迭代。 因此,在每个操作中,您将迭代计数减少一个: f(inputMatrix, iteration) { //Stop condition if (iteration <= 0) return; (no change) //... f(n1, iteration-1); f( ... -
递归和迭代(Recursion and Iteration)[2023-12-22]
我们可以区分(如在SICP中所做的)递归和迭代过程的递归和迭代过程 。 前者正如你的定义所描述的那样,递归与数学递归基本相同: 递归过程是根据自身定义的。 迭代过程用循环语句重复一段代码。 但是,递归过程是一个采用非常量(例如O(n)或O(lg(n))空间 )执行的过程,而迭代过程需要O(1)(常量)空间。 对于数学例子,斐波纳契数是递归定义的: Sigma符号类似于迭代: 正如Pi符号一样 。 类似于一些(数学)递归公式可以被重写为迭代公式,一些(但不是全部)递归流程具有迭代等价物。 所有递归过程都可以通 ... -
可能有很多原因; 一些浮现在脑海中: 递归深度是大量增加堆栈使用 - 堆叠和展开堆栈所花费的时间,堆栈消耗内存,每次递归都会减少,而迭代则使用不使用更多资源的JUMP命令(包括堆栈) 递归函数包含许多状态(例如局部变量),这些状态必须保留在内存中,直到递归完成(另一方面,迭代会在每次迭代时抛弃本地),同样为每个后续递归留下更少的可用内存 Could be many reasons; some that come to mind: The recursive depth is large increasing ...
-
将迭代转换为递归(Converting Iteration to Recursion)[2022-11-03]
正如JackVanier针对此特定问题所解释的那样,您必须将因子作为方法参数传递。 但是公共方法签名应保持不变,因此您必须编写两个新方法:一个可公开访问,具有预期签名,另一个是真正递归的,并由另一个方法调用。 public static int recursive(int x) { return recursive(x, 2); } private static int recursive(int x, int factor) { if (factor >= x) retu ... -
严格地说,当你想表达关于列表中所有(或足够多)元素的某种关系时,你怎么能避免递归。 但是,您可以将递归部分委托给某个库谓词。 以maplist-family为例。 说maplist(=(_), Xs) ,它描述了元素相同的所有列表。 对你而言,不再有任何递归。 但后面有一个递归定义: maplist(_C_1, []). maplist(C_1, [E|Es]) :- call(C_1, E), maplist(C_1, Es). 还有其他方法可以实现递归谓词的快捷方式,例如do-loops和 ...