Nathan
Nathan

Reputation: 621

#define efficiency in C

I'm new to C and was wondering which is the more efficient or preferred way to program the following:

Option A:

#define flag true

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

Option B:

#define flag

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

Upvotes: 2

Views: 434

Answers (5)

Petr Skocik
Petr Skocik

Reputation: 60058

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.


(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.)

Upvotes: 5

K J Gor
K J Gor

Reputation: 760

Suggestion:

You should use #ifdef in place of #if defined(flag).(Good Coding Practice)

and code be like:

Define:

 #define FLAG

Use:

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

Upvotes: 0

cdcdcd
cdcdcd

Reputation: 569

The #define is a precompiler directive. So whatever you write will be analyse and the code inserted.

For example, in your first example the condition will always be true. The compiler will likely just optimise this if() condition out.

Again, with second example this is a "compiler time" test (#if) not a run-time test (if).

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).

Upvotes: 1

mhawke
mhawke

Reputation: 87064

The second one results in less generated code when flag is not defined.

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.

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.

Upvotes: 3

Codor
Codor

Reputation: 17605

It depends. The code below is completely analyzed during compile-time, so doSomething will always be executed without any unnecessary cost during runtime. The upper version is expanded at compile-time into the if statement; the if condition will potentially be evaluated at runtime. 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.

Upvotes: 1

Related Questions