高效程序员 书单

    科技2022-08-03  111

    高效程序员 书单

    What does it mean to write efficient code?Efficient: achieving maximum productivity with minimum wasted effort or expense.

    编写高效的代码是什么意思?高效:以最小的工作量或费用来实现最高的生产率。

    Welcome to my humble blog. So glad you are here! Join me in a little investigation of the basics of Big O Notation and Code Efficiency.

    欢迎来到我谦虚的博客。 很高兴您在这里! 和我一起对Big O符号和代码效率的基础知识进行一些调查。

    What is Code Efficiency…

    什么是代码效率…

    Its a buzzy term that gets thrown around in meetups, lectures, and blogs. Broadly used to describe the speed and reliability of code, it is closely linked with algorithmic efficiency and the speed of runtime execution for software. In a time where AI, scalability, and Machine Learning are at the forefront of Software Development, the topic comes up time and time again.

    在聚会,演讲和博客中都充斥着一个笼统的术语。 广泛用于描述代码的速度和可靠性,它与算法效率和软件的运行时执行速度紧密相关。 在AI,可伸缩性和机器学习处于软件开发的最前沿的时代,这个话题一次又一次出现。

    What is Big O Notation…

    什么是大O符号...

    It’s a tool used in Computer Science to describe the performance and efficiency of an algorithm and analyze overall performance. It is used to determine the worst-case scenario for time required, or space used to complete execution of an algorithm. Big O is a valuable tool to determine the best implementation of a function based on performance, and provides us with a way to talk formally about how the runtime of an algorithm changes based on input.

    它是计算机科学中用于描述算法性能和效率并分析整体性能的工具。 它用于确定所需时间或用于完成算法执行的空间的最坏情况。 Big O是一种可根据性能确定最佳功能实现的有价值的工具,它为我们提供了一种正式讨论算法运行时间如何根据输入变化的方法。

    Time Complexity vs Space Complexity

    时间复杂度与空间复杂度

    Big O notation is used to measure time complexity and space complexity. Time Complexity: the amount of small operations that must be performed to complete operation.Space Complexity: the amount of additional memory that must be allocated to run the code in an algorithm. Often referred to as Auxiliary Space Complexity, meaning it refers to the space being taken up by the algorithm, not including space being taken up by the inputs

    大O符号用于测量时间复杂度和空间复杂度。 时间复杂度:完成操作必须执行的小操作的数量。空间复杂度:运行算法中的代码必须分配的额外内存的数量。 通常称为辅助空间复杂度,意味着它是指算法占用的空间,不包括输入所占用的空间

    Types of Complexity

    复杂性类型

    Time Complexity can fall within several different types. Here are a few of the more common types. 1. Constant / O(1): will always execute in the same time or space, regardless of size of the input data set. 2. Logarithmic / O(log n): the power to which a fixed number must be raised to produce a given number3. Linear / O(n): complexity will grow in direct correlation to the size of the input data4. Linearithmic / O(nlog n): performs an O(log n) operation on each item in the input.5. Quadratic / O(n²): performance is directly proportionate to the squared size of the input data

    时间复杂度可以分为几种不同的类型。 以下是一些较常见的类型。 1. 常量/ O(1) :将始终在相同的时间或空间中执行,而不管输入数据集的大小如何。 2. 对数/ O(log n) :必须增加固定数字才能产生给定数字的幂3。 线性/ O(n) :复杂度将与输入数据的大小直接相关4。 线性运算/ O(nlog n) :对输入中的每个项目执行O(log n)操作5。 二次/ O(n²) :性能与输入数据的平方大小成正比

    Diagram Courtesy of Colt Steele’s Javascript Algorithm and Data Structures Masterclass 图由Colt Steele的Javascript算法和数据结构大师班提供

    Some Helpful General Rules when Determining Time and Space Complexity …Disclaimer: these are general rules that can prove to be a helpful starting point, but they will not always work.

    确定时间和空间复杂性时的一些有用的一般规则...免责声明:这些是可以证明是有用的起点的一般规则,但它们并不总是有效。

    Time Complexity

    时间复杂度

    Arithmetic operations are constant

    算术运算是恒定的 Variable assignment is constant

    变量分配是恒定的 Accessing elements in an array (by index), or an object (by key) is constant

    访问数组中的元素(按索引)或对象(按键)是常量 In a loop the complexity is the length of the loop times the complexity of whatever happens inside that loop

    在循环中,复杂度是循环的长度乘以该循环内发生的事情的复杂度

    Space Complexity

    空间复杂度

    Most primitives are constant space. (booleans, numbers, undefined, null)

    大多数图元是恒定空间。 (布尔值,数字,未定义,空) Strings require O(n) space, where n is the length of the string

    字符串需要O(n)空间,其中n是字符串的长度 Reference types are generally O(n), where n is the length of the array or number of keys for objects.

    引用类型通常为O(n),其中n是数组的长度或对象键的数量。

    Lets Evaluate Some Examples!

    让我们评估一些例子!

    In the above example the number of operations grows roughly proportionate with n. So if n is 100, then i will be added to the total 100 times.Time complexity for addUpToN will be linear / O(n)As far as space complexity, addUpToN has 2 variable assignments(total and i). These variables get reassigned as the loop completes its operation, but the space taken up by these variables remains the same no matter what the input data set size will be.Space Complexity will be constant / O(1)

    在上面的示例中,运算数量与n大致成比例增长。 因此,如果n为100,那么我将被加到总共100次。addUpToN的时间复杂度将是线性/ O(n)就空间复杂度而言,addUpToN具有2个变量分配(总计和i)。 这些变量在循环完成操作后重新分配,但无论输入数据集的大小如何,这些变量占用的空间都将保持不变。空间复杂度将为常数/ O(1)

    Here we have 3 simple operations(multiplication, addition, division). Regardless of the size of n the number of operations remains the same.Time Complexity of addUpToNAgain is constant / O(1)There is only one value that will be returned. The input value does not change the space being allocated to this function. therefore space complexity is also linear / O(1)

    这里我们有3个简单的运算(乘法,加法,除法)。 无论n的大小如何,操作数均保持不变。addUpToNAgain的时间复杂度为常数/ O(1)将仅返回一个值。 输入值不会更改分配给该功能的空间。 因此空间复杂度也是线性的/ O(1)

    Here we have a linear O(n) operation nested within another O(n) operation. As the input n scales, the runtime squares.Time Complexity of sumEachPair is quadratic / O(n²)If we recall from the helpful general rules earlier, reference types are generally O(n), which in this case is true. The amount of space that must be allocated directly correlates to the input value. Space Complexity is linear / O(n).

    在这里,我们在另一个O(n)运算中嵌套了一个线性O(n)运算。 随着输入n的缩放,运行时平方。sumEachPair的时间复杂度为二次/ O(n²)如果我们从较早的有用通用规则中回想起,引用类型通常为O(n),在这种情况下为true。 必须分配的空间量与输入值直接相关。 空间复杂度是线性/ O(n)。

    Recap…

    回顾……

    Understanding the time and space complexity of the code we write is an important tool we can utilize to ensure we maintain the fastest runtimes and fastest execution possible, all while staying within the bounds of the physical memory of the system it is intended to run on. 1. To analyze the performance of an algorithm, we use Big O Notation2. Big O Notation can give us a high level understanding of the time and space requirements of an algorithm

    了解我们编写的代码的时间和空间复杂性是我们可以利用的重要工具,可确保我们保持最快的运行时间和最快的执行速度,同时又始终处于要运行的系统物理内存的范围内。 1.为了分析算法的性能,我们使用Big O Notation2。 大O表示法可以使我们对算法的时间和空间要求有较高的了解

    翻译自: https://medium.com/@kelly.lynn.becker/be-an-effective-and-efficient-programmer-aabde20c673e

    高效程序员 书单

    Processed: 0.009, SQL: 8