大注解和复杂性

Programmers need a way to evaluate how “good” code is. This is useful for technical interviews and generally useful for becoming a more efficient coder. Three examples of ways to evaluate code are; how long does the code take to run, how much space does the code take, or how readable is the code. The last one is subjective, however the first two ways how definitive methods to define/evaluate them. More specifically, we’d like to examine time complexity and space complexity.

程序员需要一种评估“好”代码的方式。 这对于技术面试很有用,通常对于成为更高效的编码员很有用。 评估代码的方法的三个示例是: 代码运行需要多长时间,代码需要占用多少空间或代码的可读性。 最后一个是主观的,但是前两种方式是定义/评估它们的权威方法。 更具体地说,我们想检查时间复杂度和空间复杂度。

Time complexity is defined as the delta or change in the time of execution of the code depending on the change of the input. What does this mean? Why wouldn’t we just examine how the time it takes to execute the code? I will answer the second inquiry. If we were to examine a function that takes an input of “n” that outputs “n*n”, we’d be able to calculate the execution time by evaluating the time the code starts and the time the code is executed. However, this time isn’t consistent. The same console may output different times, different consoles may output different times, and when the code is executed almost instantaneously you are dealing with precise micro-measurements and differences that you may not be able to calculate correctly or efficiently.

时间复杂度定义为取决于输入变化的代码执行时间的增量或变化。 这是什么意思? 我们为什么不只检查执行代码所需的时间呢? 我将回答第二个询问。 如果我们要检查一个输入为“ n”且输出为“ n * n”的函数,则可以通过评估代码开始的时间和代码的执行时间来计算执行时间。 但是,这次并不一致。 相同的控制台可能会输出不同的时间,不同的控制台可能会输出不同的时间,并且几乎在瞬间执行代码时,您正在处理的是精确的微尺寸和差异,您可能无法正确或高效地进行计算。

Space complexity refers to the space required by the algorithm, not including space taken up by the inputs. Primitives like Booleans, numbers, null, etc. are constant space. Strings require O(n) of space where n is the length of the string and Reference types (arrays and objects) require O(n) of space where n is the length of the array or the number of keys in an object. O(n) is Big O Notation.

空间复杂度是指算法所需的空间,不包括输入所占用的空间。 布尔值,数字,空值等基元是恒定空间。 字符串需要O(n)的空间,其中n是字符串的长度,引用类型(数组和对象)需要O(n)的空间,其中n是数组的长度或对象中键的数目。 O(n)是大O表示法。

Big O Notation is how we define the complexity of the algorithm or code, examining the relationship between how the runtime grows as the inputs grow. An algorithm is O(f(n)) if the number of simple operations the computer has to do is eventually less than a constant times f(n), as n increases. If the relationship in constant, the notation will be O(1), f(n) = 1. If the relationship is linear, the notation will be O(n), f(n) = n. And if the relationship is quadratic, the notation will be O(n2), f(n) = n2. There are even more relationships to examine, but these are the most common.

大O表示法是我们定义算法或代码复杂性的方式,它检查了运行时间随着输入的增长而增长之间的关系。 如果随着n的增加,计算机必须执行的简单操作的数量最终小于常数f(n),则算法为O(f(n))。 如果关系为常数,则符号为O(1),f(n)=1。如果关系为线性,则符号为O(n),f(n)= n。 如果关系是二次关系,则符号将为O(n2),f(n)= n2。 还有更多的关系需要检查,但这是最常见的。

翻译自: https://medium.com/@zjschulz/big-o-notation-and-complexities-13b4f74ae2c3

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值