简体   繁体   English

将几行代码写入一行是否有益(就内存和空间复杂性而言)。 这值得么?

[英]Is it beneficial(in terms of memory & space complexity) to write a few lines of code into a single line. Is it worth it?

Example: Simple program of swapping two nos. 示例:交换两个编号的简单程序。

 int a = 10;
 int b = 20;
 a = a+b;
 b = a-b;
 a = a-b;

Now in the following piece of code: 现在在下面的代码中:

 a=a+b-(b=a);

I mean What is the difference b/w these two piece of codes? 我的意思是这两个代码的黑白区别是什么?

Addition : What if the addition of these two exceed the legitimate limit of an Integer which is different in case of Java & C++? 加法:如果这两个的加法超出了Integer的合法限制(在Java和C ++情况下是不同的),该怎么办?

Neither of these looks good to me. 这些对我来说都不好。 Readability is key. 可读性是关键。 If you want to swap values, the most "obvious" way to do it is via a temporary value: 如果要交换值,最“显而易见”的方法是通过一个临时值:

int a = 10;
int b = 20;

int tmp = a;
a = b;
b = tmp;

I neither know nor would I usually care whether this was as efficient as the "clever" approaches involving arithmetic. 我既不知道,也通常不关心这是否和涉及算术的“聪明”方法一样有效。 Until someone proves that the difference in performance is significant within a real application , I'd aim for the simplest possible code that works. 除非有人证明在实际应用程序中性能差异显着,否则我将一直致力于最简单可行的代码。 Not just here, but for all code. 不仅在这里,而且对于所有代码。 Decide how well you need it to perform (and in what dimensions), test it, and change it to be more complicated but efficient if you need to . 确定您需要它执行的性能(以及在什么尺寸上),对其进行测试,并根据需要将其更改为更复杂但更有效的方法

(Of course, if you've got a swap operation available within your platform, use that instead... even clearer.) (当然,如果平台中有可用的swap操作,请改用它,甚至更清楚。)

In C++, the code yields undefined behavior because there's no sequence point in a+b-(b=a) and you're changing b and reading from it. 在C ++中,代码产生未定义的行为,因为a+b-(b=a)没有序列点,并且您正在更改b并从中读取。

You're better off using std::swap(a,b) , it is optimized for speed and much more readable than what you have there. 您最好使用std::swap(a,b) ,它已针对速度进行了优化,并且比您所拥有的更具可读性

Since your specific code is already commented upon, i would just add a general aspect. 由于您的特定代码已被注释,因此我只添加一个一般方面。 Writing one liners doesn't really matter because at instruction level, you cannot escape the number of steps your assembly is going to translate into machine code. 编写一个衬板并不重要,因为在指令级别,您无法逃避汇编将转换为机器代码的步骤数。 Most of the compilers would already optimize accordingly. 大多数编译器已经进行了相应的优化。

That is, unless the one liner is actually using a different mechanism to achieve the goal for eg in case of swapping two variables, if you do not use a third variable and can avoid all the hurdles such as type overflow etc. and use bitwise operators for instance, then you might have saved one memory location and thereby access time to it. 也就是说,除非一个内联函数实际上使用了不同的机制来实现例如在交换两个变量的情况下的目标,否则,如果您不使用第三个变量,并且可以避免所有障碍(例如类型溢出等)并使用按位运算符例如,那么您可能已经保存了一个存储位置,从而可以访问它。

In practice, this is of almost no value and is trouble for readability as already mentioned in other answers. 实际上, 这几乎没有任何价值,并且在可读性方面也有麻烦,正如其他答案中已经提到的那样。 Professional programs need to be maintained by people so they should be easy to understand. 人们需要维护专业程序,因此它们应该易于理解。

One definition of good code is Code actually does what it appears to be doing 好的代码的一种定义是, 代码实际上会执行它似乎正在做的事情

Even you yourself would find it hard to fix your own code if it is written cleverly in terms of some what shortened but complex operations. 如果您巧妙地编写了一些缩短了但又复杂的操作的代码,即使您自己也将发现很难修复自己的代码。 Readability should always be prioritized and most of the times, the real needed efficiency comes from improving design, approach or better data structures/algorithms, than instead short - one liners. 可读性应始终放在优先位置,在大多数情况下,真正需要的效率来自改进设计,方法或更好的数据结构/算法,而不是短线。

Quoting Dijkstra : The competent programmer is fully aware of the limited size of his own skull. 引用Dijkstra称职的程序员完全意识到自己头骨的大小。 He therefore approaches his task with full humility, and avoids clever tricks like the plague. 因此,他满怀谦卑地完成自己的任务,并避免了诸如瘟疫之类的巧妙技巧。

A couple points: 几点:

  • Code should first reflect your intentions . 代码首先应该反映您的意图 After all, it's meant for humans to read. 毕竟,它是供人类阅读的。 After that, if you really really must, you can start to tweak the code for performance. 在那之后,如果确实需要,则可以开始调整代码以提高性能。 Most of all never write code to demonstrate a gimmick or bit twiddling hack. 大多数人从不编写代码来证明a头或有点不休。
  • Breaking code onto multiple lines has absolutely no impact on performance. 将代码分成多行绝对不会影响性能。
  • Don't underestimate the compiler's optimizer. 不要小看编译器的优化器。 Just write the code as intuitively as possible, and the optimizer will ensure it has the best performance. 只需尽可能直观地编写代码,优化器将确保其具有最佳性能。

In this regard, the most descriptive, intuitive, fastest code, is: 在这方面,最具描述性,直观性和最快的代码是:

std::swap(a, b);

Readability and instant understand-ability is what I personally rate (and several others may vote for) when writing and reading code. 可读性和即时理解性是我个人在编写和阅读代码时的评价(以及其他一些人可能会赞成的)。 It improves maintainability. 它提高了可维护性。 In the particular example provided, it is difficult to understand immediately what the author is trying to achieve in those few lines. 在提供的特定示例中,很难立即理解作者在这几行中想要实现的目标。 The single line code: a=a+b-(b=a); 单行代码: a=a+b-(b=a); although very clever does not convey the author's intent to others obviously. 尽管非常聪明,但显然不能将作者的意图传达给他人。

In terms of efficiency, optimisation by the compiler will achieve that anyway. 就效率而言,编译器的优化无论如何都会实现这一点。

In terms of java at least i remember reading that the JVM is optimized for normal straight forward uses so often times you just fool yourself if you try to do stuff like that. 至少在Java方面,我记得读过JVM针对常规的直接使用进行了优化,因此,如果您尝试执行此类操作,那么您常常会自欺欺人。

Moreover it looks awful. 而且它看起来糟透了。

OK, try this. 好,试试看 Next time you have a strange bug, start by squashing up as much code into single lines as you can. 下次遇到一个奇怪的错误时,请先将尽可能多的代码压缩为一行。

Wait a couple weeks so you've forgotten how it's supposed to work. 请等待几周,以免您忘记了它应该如何工作。

Try to debug it. 尝试调试它。

Of course it depends on the compiler. 当然,这取决于编译器。 Although I cannot foresee any kind of earth-shattering difference. 尽管我无法预见到任何令人震惊的差异。 Abstruse code is the main one. 恶意代码是主要代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM