32位系统能够识别多达内存
This story is all about code performance, we can see how do we write the code in a traditional way or when we have a short deadline on our head and how to optimize that code to run fast and make it more readable to other fellow developers.
这个故事全都是关于代码性能的,我们可以看到我们如何用传统的方式编写代码,或者当我们的工作期限很短时,以及如何优化该代码以使其快速运行并使其对其他开发人员更具可读性。
“Any fool can write code that a computer can understand. Good programmers write code that humans can understand” — Martin Fowler
任何傻瓜都可以编写计算机可以理解的代码。 优秀的程序员编写人类可以理解的代码” – Martin Fowler
That’s great. But you said, “optimize that code” so my question is: “what is code optimization?”
那很棒。 但是您说过“优化代码”,所以我的问题是: “什么是代码优化?”
代码优化 (Code Optimization)
Code optimization is, any method of code modification to improve code quality and efficiency. A program may be optimized so that it becomes a smaller in size, consumes less memory, executes more rapidly.
代码优化是用于提高代码质量和效率的任何代码修改方法。 可以对程序进行优化,以使其变得更小,消耗更少的内存,执行得更快。
I believe we are pretty much clear about code optimization right? Yup!
我相信我们对代码优化非常清楚,对吗? 对!
Let’s take a problem and start building a solution for that: “Write a function to determine whether the string is a palindrome or not?”
让我们解决一个问题并开始为此建立解决方案:“ 编写一个函数来确定字符串是否是回文?”
回文 (Palindromes)
A palindrome is a word, number, phrase, or other sequences of character which reads the same backward as forward, such as madam, racecar.
回文是指单词,数字,短语或其他字符序列,例如,女士,赛车等。
![Palindrome family](https://miro.medium.com/max/9999/1*TzPS3_LdNMfE0y6d4dguJw.jpeg)
Let’s start coding!
让我们开始编码!
Well, I code in JavaScript so that’s why I am using JavaScript in this example but you may use any of your favorite programming languages, it’s totally up to you.
嗯,我使用JavaScript编写代码,因此这就是我在此示例中使用JavaScript的原因,但是您可以使用任何喜欢的编程语言,这完全取决于您。
What we do in a traditional way.
我们以传统方式所做的事情。
function isPalindrome(str) {return str.toLowerCase() === str.toLowerCase().split('').reverse().join('');
};
isPalindrome('madam'); // trueisPalindrome('racecar'); // trueisPalindrome('pop'); // trueisPalindrome('something'); // falseisPalindrome('abc'); // false
I have done it, it was super easy. In the above code snippet, there is a function that takes a string as input after doing some processing over that input and finally returns the output whether the string is palindrome or not.
我已经做到了,这非常容易。 在上面的代码段中,有一个函数在对该输入进行了一些处理之后将字符串作为输入,并最终返回输出,无论该字符串是否为回文。
I have also run some test cases as you can see above and they work totally fine.
您还可以在上面运行一些测试用例,它们可以正常工作。
This is what we do in a hurry. Let’s try to optimize this. 🤔
这是我们急着要做的。 让我们尝试对其进行优化。 🤔
function isPalindrome(str) {
str = str.toLowerCase();return str === str.split('').reverse().join('');
};
Yay, I have optimized the above code block.Combined two 𝚝𝚘𝙻𝚘𝚠𝚎𝚛𝙲𝚊𝚜𝚎 function calls into a one and it increased our code performance, you may check the attached screenshots.
是的,我已经优化了上面的代码块,将两个𝚝𝚘𝙻𝚘𝚠𝚎𝚛𝙲𝚊𝚜𝚎函数调用合并为一个,这提高了我们的代码性能,您可以查看随附的屏幕截图。
![Image for post](https://miro.medium.com/max/9999/1*TJParWhak7F7HXr-EAekFA.png)
![Image for post](https://miro.medium.com/max/9999/1*dET9SC9cyLsXsmszmeYuIQ.png)
绩效公式 (Performance Formula)
The formula is written in the above screenshot but lemme explain to you here.Let’s say code block #1 executes in 4ms while the code block #2 in 3ms so our statement would be like this:min_time / max_time * 1003 / 4 * 100 = 75 100 - 75 = 25
该公式写在上面的屏幕截图中,但我在这里向您解释。假设代码块#1在4毫秒内执行,而代码块#2在3毫秒内执行,因此我们的语句如下: min_time / max_time * 1003 / 4 * 100 = 75 100 - 75 = 25
This means code block #2 is 25% faster than code block #1
这意味着代码块2比代码块1快25%
As per the above performance result, isPalindrome1
is executed in 0.01074ms while isPalindrome2
is executed in 0.00610ms that means isPalindrome2
is 43% faster.
根据以上性能结果, isPalindrome1
的执行时间为0.01074ms,而isPalindrome2
的执行时间为0.00610ms,这意味着isPalindrome2
执行速度提高了43%。
Cool, increased almost 40% of performance just combining two separate calls into a one, this is what we never think while coding. Yeah, you are right.
酷,仅将两个单独的调用合并为一个,就提高了近40%的性能,这是我们在编码时从未想到的。 是的,没错。
It’s time to make it more speedy and readable also. I know “split(‘’).reverse().join(‘’)” sounds a bit confusing.
现在该使它变得更加快速和可读了。 我知道“ split('')。reverse()。join('') ”听起来有点混乱。
Let’s come up with a better approach to do this. 🤔
让我们提出一种更好的方法来做到这一点。 🤔
I have come up with an algorithm, using this algorithm we can make our solution more speedy and readable to other fellows. The algorithm is super flexible can be implemented in any programming language.
我想出了一种算法,使用该算法,我们可以使我们的解决方案更加快速,其他人也可以理解。 该算法超级灵活,可以用任何编程语言实现。
Let me show you the flowchart diagram of the algorithm so that you can better understand it.
让我向您展示该算法的流程图,以便您可以更好地理解它。
![Palindrome Algorithm](https://miro.medium.com/max/9999/1*zxpcei68lcRIeez-dEugCw.png)
流程图。 (Flowchart.)
A flowchart is a type of diagram that represents a workflow or process.A flowchart can also be defined as a diagrammatic representation of an algorithm, a step-by-step approach to solving a task.
流程图是表示工作流或过程的一种图类型。流程图也可以定义为算法的图形表示形式,是解决任务的分步方法。
Let’s build a solution using the above algorithm and see what is the result.
让我们使用上述算法构建一个解决方案,看看结果如何。
function isPalindrome(str) {//converting string into lowercasestr = str.toLowerCase();//divid string by 2, floor the value and saving it to countlet count = Math.floor(str.length / 2);// initialize flag with default value of true let flag = true;//loop over the countfor(var i = 0; i < count; i++){//comparing string charactersif(str[i] !== str[str.length - i - 1]){//set flag to false and break the loopflag = false;console.log('No palindrome found');break;
}
}return flag;
}
I think we should run all the above test cases against this program to check whether it is working as expected or not.
我认为我们应该针对该程序运行上述所有测试用例,以检查其是否按预期工作。
Hmmm, good idea!
嗯,好主意!
isPalindrome('abc'); // falseisPalindrome('madam'); // trueisPalindrome('racecar'); // trueisPalindrome('pop'); // trueisPalindrome('something'); // falseisPalindrome('xyz'); // false
Awesome, let’s check the performance.
太好了,让我们检查一下性能。
![Image for post](https://miro.medium.com/max/9999/1*FiV4kq0yRaYjWtYA5PkS9Q.png)
![Image for post](https://miro.medium.com/max/9999/1*fsW7jPJ-uWDUYpPGKxoTfQ.png)
As per the above performance result, isPalindrome3
almost 4% faster than isPalindrome2
in both cases, while 43% faster than isPalindrome1
in the worst-case scenario and almost 55% faster in the best-case scenario.
按照上述执行结果, isPalindrome3
近4%的速度比isPalindrome2
在这两种情况下,43%的速度比isPalindrome1
在最坏的情况下,几乎55%的在最好的情况下更快。
That’s awesome!
棒极了!
结论。 (Conclusion.)
In this story, we have learned what code optimization is, how to optimize the code and make it more readable to others.
在这个故事中,我们学习了什么是代码优化,如何优化代码并使之更易于他人阅读。
We came up with a solution for a given problem, then optimized that solution, finally come up with an algorithm to maximize the speed, this algorithm further improved our code quality, performance, readability.
我们针对给定的问题提出了解决方案,然后对该解决方案进行了优化,最后提出了一种使速度最大化的算法,该算法进一步提高了代码质量,性能和可读性。
32位系统能够识别多达内存