简体   繁体   English

"int a, b;" 之间的性能差异和“int a; int b;”?

[英]Performance difference between "int a, b;" and "int a; int b;"?

It's an easily described in question as the title says.正如标题所说,这是一个很容易描述的问题。

This question came to my mind every time I can put two separate statements together in a block.每次我可以将两个单独的语句放在一个块中时,我就会想到这个问题。 Especially when I code for CPU cost problems like NP-complete problems.特别是当我为 CPU 成本问题(如 NP 完全问题)编码时。

Is there any, necessary to combine two separate statements together?.是否有必要将两个单独的语句组合在一起?

Performance difference between “int a, b;” “int a, b;”之间的性能差异and “int a;和“int a; int b;”?国际b;”?

Those declarations are semantically equivalent, and there is no reason why either would generate different program from the other, and therefore no reason why there would be difference in performance.这些声明在语义上是等价的,没有任何理由会生成与另一个不同的程序,因此没有理由会导致性能差异。

However, the first declaration is shorter by a few characters, so the compilation process might be a few micro-seconds faster for the time that's needed to load the source file from the disk.但是,第一个声明要短几个字符,因此在从磁盘加载源文件所需的时间内,编译过程可能会快几微秒。

Is there any necessary to combine two separate statements together?是否有必要将两个单独的语句组合在一起?

In general, no.一般来说,没有。

As others have stated, both are equivalent declarations.正如其他人所说,两者都是等效的声明。 One may compile slightly faster than the other.一个可能比另一个编译稍快。

They are both declarations .它们都是声明
Declarations can be implemented at run-time by allocating space in memory (such as on the stack), reserving a register, or ignored.声明可以在运行时通过在内存中分配空间(例如在堆栈上)、保留寄存器或忽略来实现。

If the compiler emits instructions for the variable allocations, the processing time will be negligible (in the order of nanoseconds or microseconds).如果编译器发出变量分配指令,处理时间将可以忽略不计(以纳秒或微秒为单位)。

In considering performance, your first check list item should be (in the following order):在考虑性能时,您的第一个检查清单项目应该是(按以下顺序):

  1. Requirements - Elimination of requirements can speed up a program.需求 - 消除需求可以加速程序。
  2. Design - Efficient algorithms, removal of variables, efficient interfaces设计 - 高效算法,去除变量,高效接口
  3. Implementation (coding) - Efficient coding of algorithms.实现(编码) - 算法的高效编码。
  4. Compiler Optimization levels -- Setting compiler optimization levels at their highest.编译器优化级别——将编译器优化级别设置为最高。
  5. Platform specific tricks -- Using special processor algorithms, hardware support, etc.特定于平台的技巧——使用特殊的处理器算法、硬件支持等。

Usually, customers would rather have a slower higher quality program than a really fast unreliable program.通常,客户宁愿使用速度较慢的高质量程序,也不愿使用速度非常快且不可靠的程序。
Stakeholders would rather have a program developed quicker than paying for optimizations that don't have a high Return On Investment (ROI).利益相关者宁愿更快地开发一个程序,也不愿为没有高投资回报 (ROI) 的优化付费。

Focus on quality and robustness.专注于质量和稳健性。 Optimize only when necessary.仅在必要时进行优化。

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

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