CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Threaded View

  1. #1
    Join Date
    Jan 2009
    Posts
    1,689

    Which offers the best performance?

    I have a library that I am trying to clean up (it's got way too much preprocessor in it.) I wanted to ask here to be sure that the replacements I'm making will not show any performance problems.

    Preprocessor
    Code:
    #define AMOUNT 5
    #define INC
    
    int foo(int i){
       #ifdef INC
          return i + AMOUNT;
       #else
          return i - AMOUNT;
       #endif
    }
    Consts
    Code:
    const int AMOUNT = 5;
    const bool INC = true;
    
    int foo(int i){
       if (INC){
          return i + AMOUNT;
       }
       return i - AMOUNT;  //not in an else to prevent compiler warnings about reaching end of non-void method
    }
    Templates
    Code:
    template <bool INC, int AMOUNT>
    int foo(int i){
       if (INC){
          return i + AMOUNT;
       }
       return i - AMOUNT;  //not in an else to prevent compiler warnings about reaching end of non-void method
    }
    Each of these approaches should end up producing the same instructions correct? All of the decisions end up being made at compile time and the compiler will remove the compiled code that is inaccessible. I will end up using a mix of const and templates depending on the circumstances of the change, but since it's a time critical library, I want to make sure there is no performance degradation.
    Last edited by ninja9578; March 28th, 2012 at 11:53 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured