32位系统能够识别多达内存_优化您的代码,它可以更快地执行多达55次️

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?”

那很棒。 但是您说过“优化代码”,所以我的问题是: “什么是代码优化?”

Image for post

代码优化 (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
All the names are palindrome
所有的名字都是回文

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
isPalindrom1 vs isPalindrome2 (input: “something”)
isPalindrom1 vs isPalindrome2(输入:“ something”)
Image for post
isPalindrom1 vs isPalindrome2 (input: “madam”)
isPalindrom1和isPalindrome2(输入:“女士”)

绩效公式 (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. 🤔

让我们提出一种更好的方法来做到这一点。 🤔

Image for post
Idea
理念

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

流程图。 (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
isPalindrome1 vs isPalindrome2 vs isPalindrome3 (input: “something”) best-case scenario
isPalindrome1 vs isPalindrome2 vs isPalindrome3(输入:“某物”)最佳情况
Image for post
isPalindrom1 vs isPalindrome2 vs isPalindrome3 (input: “madam”) worst-case scenario
isPalindrom1 vs isPalindrome2 vs isPalindrome3(输入:“女士”)最坏情况

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.

我们针对给定的问题提出了解决方案,然后对该解决方案进行了优化,最后提出了一种使速度最大化的算法,该算法进一步提高了代码质量,性能和可读性。

翻译自: https://medium.com/javascript-in-plain-english/optimize-your-code-and-itll-execute-up-to-55-faster-️-3f0c1929ab07

32位系统能够识别多达内存

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值