简体   繁体   English

#define在C中的效率

[英]#define efficiency in C

I'm new to C and was wondering which is the more efficient or preferred way to program the following: 我是C的新手,并且想知道哪个是更有效或首选的方式来编程以下内容:

Option A: 选项A:

#define flag true

void Foo()
{
   for (size_t i = 0; i < veryBigNumber; i++)
   {
      if (flag)
         doSomething1();
      doSomething2();
   }
}

Option B: 选项B:

#define flag

void Foo()
{
   for (size_t i = 0; i < veryBigNumber; i++)
   {
#if defined(flag)
         doSomething1();
#endif
      doSomething2();
   }
}

Unless you're on a very archaic/dumb compiler, they should be equivalent. 除非你是一个非常古老/愚蠢的编译器,否则它们应该是等价的。 #if guarantees evaluation at compile time, however any modern compiler will evaluate a regular if with an integer constant at compile time too. #if保证在编译时进行评估,但是任何现代编译器都会在编译时使用整数常量来计算常规if


(In any case, you're only a step away from finding out for yourself. Just put a very big number in veryBigNumber and time each of the two variants. If very big number is really very big (millions, billions) even timing it externally ( time ./a.out from the command line) should give you fairly reliable timings.) (无论如何,你只需要一步之遥就可以找到自己。只需在veryBigNumberveryBigNumber一个非常大的数字并计算两个变体中的每一个。如果非常大的数字真的非常大(数百万,数十亿)甚至计时外部(来自命令行的time ./a.out )应该给你相当可靠的时间。)

The second one results in less generated code when flag is not defined. 第二个导致在未定义flag时生成的代码较少。

Importantly the code that is omitted is the if statement, so run time efficiency will also be better because the if statement is not unnecessarily being executed veryBigNumber times. 重要的是省略的代码是if语句,因此运行时效率也会更好,因为if语句不会被不必要地执行veryBigNumber次。

Compilers might be able to optimise away the if statement by analysing the code, but explicitly "ifdefing" out the code will ensure that it is. 编译器可能能够通过分析代码来优化if语句,但显式地“ifdefing”代码将确保它是。

It depends. 这取决于。 The code below is completely analyzed during compile-time, so doSomething will always be executed without any unnecessary cost during runtime. 下面的代码在编译期间被完全分析,因此doSomething将始终在运行时没有任何不必要的成本执行。 The upper version is expanded at compile-time into the if statement; 上层版本在编译时扩展到if语句; the if condition will potentially be evaluated at runtime. if条件可能会在运行时进行评估。 However, if code optimization is activated, the compiler might decide that the if condition can never be false, hence the entire if statement might be removed. 但是,如果激活了代码优化,编译器可能会决定if条件永远不会为false,因此可能会删除整个if语句。

The #define is a precompiler directive. #define是一个预编译器指令。 So whatever you write will be analyse and the code inserted. 所以无论你写什么,都会分析并插入代码。

For example, in your first example the condition will always be true. 例如,在您的第一个示例中,条件始终为true。 The compiler will likely just optimise this if() condition out. 编译器可能只是优化if if()条件。

Again, with second example this is a "compiler time" test (#if) not a run-time test (if). 同样,对于第二个示例,这是“编译时间”测试(#if)而不是运行时测试(如果)。

https://www.tutorialspoint.com/cprogramming/c_preprocessors.htm https://www.tutorialspoint.com/cprogramming/c_preprocessors.htm

Performance can be improved by using macros because they help organise your code in a similar way as a function call but the macro code is "inlined" by the compiler. 使用宏可以提高性能,因为它们有助于以与函数调用类似的方式组织代码,但宏代码由编译器“内联”。 This reduces the overhead of a jump to a location in memory that is required during run-time if a function call is used instead. 如果使用函数调用,这可以减少跳转到运行时所需的内存中的位置的开销。

They also help make it easier to deal with code that is held constant. 它们还有助于更轻松地处理保持不变的代码。 This makes your code easier to read and less painful to write (imagine having to remember and insert a constant value every-time you have to use it). 这使得您的代码更易于阅读并且编写起来更少痛苦(想象一下,每次必须使用它时都必须记住并插入常量值)。

Suggestion: 建议:

You should use #ifdef in place of #if defined(flag).(Good Coding Practice) 您应该使用#ifdef代替#if defined(flag)。(良好的编码实践)

and code be like: 和代码如下:

Define: 限定:

 #define FLAG

Use: 采用:

#ifdef FLAG
     doSomething1();
#else
    doSomething2();
#endif

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

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