CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2006
    Posts
    36

    logging inside loop

    Hi,

    I have this function to calculate the standard deviation, I need to output (logging) intermediate values inside the for loop if the variable log is true, otherwise if log is false, i will output nothing. However, I cannot use if(log == true) inside the loop, and I cannot duplicate the code for when (log == false).

    I'd appreciatte some help. Thanks

    Code:
    double StdDev(const std::vector<double> &input, bool log){ 
      int elements = input.size();
    
      double sum = 0;
      double sumsq = 0;
      
      for(int i = 0; i < elements; ++i) {
        sum += input[i];
        sumsq += input[i]*input[i];
      }
      
      double mean = sum/elements;
      double variance = sumsq / elements - mean*mean;
      return sqrt(variance);
    }
    Last edited by acosgaya; February 19th, 2011 at 01:21 PM.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: logging inside loop

    hmm... one idea that pops into my mind is something like this:
    Code:
    template<bool log>
    double StdDev(const std::vector<double> &input){
      int elements = input.size();
    
      double sum = 0;
      double sumsq = 0;
    
      for(int i = 0; i < elements; ++i) {
        sum += input[i];
        sumsq += input[i]*input[i];
    
        if(log) {
          // ...
        }
      }
    
      double mean = sum/elements;
      double variance = sumsq / elements - mean*mean;
      return sqrt(variance);
    }
    
    double StdDev(const std::vector<double> &input, bool log){
      return log ? StdDev<true>(input) : StdDev<false>(input);
    }
    The idea being that a compiler should be able to optimise out the check for log in the function template since the value of log is known at compile time. Personally, if the logging only happens in the loop body, and given that the loop is pretty small and not too complicated, I would just duplicate the loop. Or, it may turn out that just checking for log in the loop isn't so bad after all.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Feb 2011
    Posts
    4

    Re: logging inside loop

    Thanks for the comments,
    I'm not sure I got your ideas right, however, I cannot pay the cost of an "if(log)" condition inside a loop, and I cannot duplicate code.

  4. #4
    Join Date
    Jan 2006
    Location
    Belo Horizonte, Brazil
    Posts
    405

    Re: logging inside loop

    Quote Originally Posted by acxx78 View Post
    Thanks for the comments,
    I'm not sure I got your ideas right, however, I cannot pay the cost of an "if(log)" condition inside a loop, and I cannot duplicate code.
    The point is there will be no cost for the if (log) part, as long as you are using a decent compiler that will optimize the jump away (assuming an optimized release built). The templates are instantiated at compile-time.
    Last edited by ltcmelo; February 19th, 2011 at 01:53 PM.

  5. #5
    Join Date
    Feb 2011
    Posts
    4

    Re: logging inside loop

    got it, thanks guys

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