编写更短的代码/算法,是否更高效(性能)? [英] Writing shorter code/algorithms, is more efficient (performance)?

查看:48
本文介绍了编写更短的代码/算法,是否更高效(性能)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在网站上看到代码高尔夫琐事后,很明显人们试图找到在字符、行和总大小方面尽可能短地编写代码和算法的方法,即使这意味着编写如下内容:

After coming across the code golf trivia around the site it is obvious people try to find ways to write code and algorithms as short as the possibly can in terms of characters, lines and total size, even if that means writing something like:

    //Code by: job
    //Topic: Code Golf - Collatz Conjecture
    n=input()
    while n>1:n=(n/2,n*3+1)[n%2];print n

所以作为初学者,我开始怀疑尺寸是否真的很重要 :D

So as a beginner I start to wonder whether size actually matters :D

这显然是一个非常主观的问题,高度依赖于所使用的实际代码,但现实世界中的经验法则是什么.

It is obviously a very subjective question highly dependent on the actual code being used, but what is the rule of thumb in the real world.

既然大小无关紧要,那我们为什么不更关注性能而不是大小?

In the case that size wont matter, how come then we don't focus more on performance rather than size?

推荐答案

我希望这不会变成一场激烈的战争.好的代码有很多属性,包括:

I hope this does not become a flame war. Good code has many attributes, including:

  1. 正确解决用例.
  2. 可读性.
  3. 可维护性.
  4. 性能.
  5. 可测试性.
  6. 低内存签名.
  7. 良好的用户界面.
  8. 可重用性.

在 21 世纪的编程中,代码的简洁性并不重要.当内存真的很稀缺时,它曾经更为重要.有关引用上述属性的书籍,请参阅这个问题,包括我的回答.

The brevity of code is not that important in 21st century programming. It used to be more important when memory was really scarce. Please see this question, including my answer, for books referencing the attributes above.

这篇关于编写更短的代码/算法,是否更高效(性能)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆