Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Slope method optimisation

Slope method optimisation

Scheduled Pinned Locked Moved C#
comdata-structurestoolshelpquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    A
    wrote on last edited by
    #1

    I'm trying to optimise the following Slope method so that it runs in as few clock cycles as possible. Any Suggestions?

        public static double?\[\] slopeImpl(double?\[\] vals, int offset)
        {
            double y = 0;
    
            int iterableLength = vals.Length;
    
            int numValues = vals.Length;
            double invNumValues = 1.0 / (vals.Length - offset);
            double x = (double)(((numValues \* (numValues - 1)) >> 1) - ((offset \* (offset - 1)) >> 1));
    
            for (int i = offset; i < iterableLength; i++) {
    
                y += (vals\[i\].GetType() == typeof(System.DBNull)) ? 0.0 : (double)vals\[i\];
            }
    
            y = y \* invNumValues;
    
            double v1 = 0.0;
            double v2 = 0.0;
            double v2HalfResult = 0.0;
    
            for (int i = offset; i < iterableLength; i++) {
                v2HalfResult = (i - x);
                v1 += (v2HalfResult) \* ((double)vals\[i\] - y);
                v2 += (v2HalfResult) \* (v2HalfResult);
            }
    
            double slope = v1 / v2;
            double intercept = y - slope \* x;
    
            double?\[\] result = new double?\[2\];
            result\[0\] = slope;
            result\[1\] = intercept;
            return result;
        }
    

    What I have considered: 1. There are two loops which are iterating the same number of times; however I am not sure if it is possible to use one loop. The second loop is dependant on the value of y which is calculated in the first loop and the line after. 2. Use a table to calculate x which is of the form n(n-1) /2. Possible, however the values array can contain more than 365 elements. Thanks for any help.

    My blog:[^]

    R L B 3 Replies Last reply
    0
    • A A

      I'm trying to optimise the following Slope method so that it runs in as few clock cycles as possible. Any Suggestions?

          public static double?\[\] slopeImpl(double?\[\] vals, int offset)
          {
              double y = 0;
      
              int iterableLength = vals.Length;
      
              int numValues = vals.Length;
              double invNumValues = 1.0 / (vals.Length - offset);
              double x = (double)(((numValues \* (numValues - 1)) >> 1) - ((offset \* (offset - 1)) >> 1));
      
              for (int i = offset; i < iterableLength; i++) {
      
                  y += (vals\[i\].GetType() == typeof(System.DBNull)) ? 0.0 : (double)vals\[i\];
              }
      
              y = y \* invNumValues;
      
              double v1 = 0.0;
              double v2 = 0.0;
              double v2HalfResult = 0.0;
      
              for (int i = offset; i < iterableLength; i++) {
                  v2HalfResult = (i - x);
                  v1 += (v2HalfResult) \* ((double)vals\[i\] - y);
                  v2 += (v2HalfResult) \* (v2HalfResult);
              }
      
              double slope = v1 / v2;
              double intercept = y - slope \* x;
      
              double?\[\] result = new double?\[2\];
              result\[0\] = slope;
              result\[1\] = intercept;
              return result;
          }
      

      What I have considered: 1. There are two loops which are iterating the same number of times; however I am not sure if it is possible to use one loop. The second loop is dependant on the value of y which is calculated in the first loop and the line after. 2. Use a table to calculate x which is of the form n(n-1) /2. Possible, however the values array can contain more than 365 elements. Thanks for any help.

      My blog:[^]

      R Offline
      R Offline
      RobCroll
      wrote on last edited by
      #2

      Instead of

      y += (vals[i].GetType() == typeof(System.DBNull)) ? 0.0 : (double)vals[i];

      could be changed to something along the lines of

      if (vals[i].HasValue) y += vals[i].Value;

      But are there actually any null values? Changing the argument to just a straight out double[] would definitely help

      "You get that on the big jobs."

      A 1 Reply Last reply
      0
      • A A

        I'm trying to optimise the following Slope method so that it runs in as few clock cycles as possible. Any Suggestions?

            public static double?\[\] slopeImpl(double?\[\] vals, int offset)
            {
                double y = 0;
        
                int iterableLength = vals.Length;
        
                int numValues = vals.Length;
                double invNumValues = 1.0 / (vals.Length - offset);
                double x = (double)(((numValues \* (numValues - 1)) >> 1) - ((offset \* (offset - 1)) >> 1));
        
                for (int i = offset; i < iterableLength; i++) {
        
                    y += (vals\[i\].GetType() == typeof(System.DBNull)) ? 0.0 : (double)vals\[i\];
                }
        
                y = y \* invNumValues;
        
                double v1 = 0.0;
                double v2 = 0.0;
                double v2HalfResult = 0.0;
        
                for (int i = offset; i < iterableLength; i++) {
                    v2HalfResult = (i - x);
                    v1 += (v2HalfResult) \* ((double)vals\[i\] - y);
                    v2 += (v2HalfResult) \* (v2HalfResult);
                }
        
                double slope = v1 / v2;
                double intercept = y - slope \* x;
        
                double?\[\] result = new double?\[2\];
                result\[0\] = slope;
                result\[1\] = intercept;
                return result;
            }
        

        What I have considered: 1. There are two loops which are iterating the same number of times; however I am not sure if it is possible to use one loop. The second loop is dependant on the value of y which is calculated in the first loop and the line after. 2. Use a table to calculate x which is of the form n(n-1) /2. Possible, however the values array can contain more than 365 elements. Thanks for any help.

        My blog:[^]

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        No you can't avoid the two loops, you need the averages first, then the deviations, which you can't get without knowing the averages first. IMO all you can do is clean up your code a bit, by: - getting rid of the nullables, GetType() and typeof; - not calling vals.Length three times; - returning just the slope as a double; - not calculating the intercept, unless you really need it, and then maybe provide the intercept as an out parameter (this saves the allocation of the double[2] array). BTW: using >>1 to divide by 2 does not really help; compilers are smart enough to emit decent code to divide by 2. BTW2: I have some doubts about the correctness of your line double x = (double)(((numValues * (numValues - 1)) >> 1) - ((offset * (offset - 1)) >> 1)); it doesn't agree with my intuition, e.g. changing the value of offset from 0 to 1 would not change x at all?? :)

        Luc Pattyn [My Articles] Nil Volentibus Arduum

        A 1 Reply Last reply
        0
        • A A

          I'm trying to optimise the following Slope method so that it runs in as few clock cycles as possible. Any Suggestions?

              public static double?\[\] slopeImpl(double?\[\] vals, int offset)
              {
                  double y = 0;
          
                  int iterableLength = vals.Length;
          
                  int numValues = vals.Length;
                  double invNumValues = 1.0 / (vals.Length - offset);
                  double x = (double)(((numValues \* (numValues - 1)) >> 1) - ((offset \* (offset - 1)) >> 1));
          
                  for (int i = offset; i < iterableLength; i++) {
          
                      y += (vals\[i\].GetType() == typeof(System.DBNull)) ? 0.0 : (double)vals\[i\];
                  }
          
                  y = y \* invNumValues;
          
                  double v1 = 0.0;
                  double v2 = 0.0;
                  double v2HalfResult = 0.0;
          
                  for (int i = offset; i < iterableLength; i++) {
                      v2HalfResult = (i - x);
                      v1 += (v2HalfResult) \* ((double)vals\[i\] - y);
                      v2 += (v2HalfResult) \* (v2HalfResult);
                  }
          
                  double slope = v1 / v2;
                  double intercept = y - slope \* x;
          
                  double?\[\] result = new double?\[2\];
                  result\[0\] = slope;
                  result\[1\] = intercept;
                  return result;
              }
          

          What I have considered: 1. There are two loops which are iterating the same number of times; however I am not sure if it is possible to use one loop. The second loop is dependant on the value of y which is calculated in the first loop and the line after. 2. Use a table to calculate x which is of the form n(n-1) /2. Possible, however the values array can contain more than 365 elements. Thanks for any help.

          My blog:[^]

          B Offline
          B Offline
          BobJanova
          wrote on last edited by
          #4

          Using double not double?, and if necessary using double.NaN for missing values, should help considerably.

          1 Reply Last reply
          0
          • R RobCroll

            Instead of

            y += (vals[i].GetType() == typeof(System.DBNull)) ? 0.0 : (double)vals[i];

            could be changed to something along the lines of

            if (vals[i].HasValue) y += vals[i].Value;

            But are there actually any null values? Changing the argument to just a straight out double[] would definitely help

            "You get that on the big jobs."

            A Offline
            A Offline
            A
            wrote on last edited by
            #5

            Thanks for the help, and yes the vals array can contain null values.

            My blog:[^]

            1 Reply Last reply
            0
            • L Luc Pattyn

              No you can't avoid the two loops, you need the averages first, then the deviations, which you can't get without knowing the averages first. IMO all you can do is clean up your code a bit, by: - getting rid of the nullables, GetType() and typeof; - not calling vals.Length three times; - returning just the slope as a double; - not calculating the intercept, unless you really need it, and then maybe provide the intercept as an out parameter (this saves the allocation of the double[2] array). BTW: using >>1 to divide by 2 does not really help; compilers are smart enough to emit decent code to divide by 2. BTW2: I have some doubts about the correctness of your line double x = (double)(((numValues * (numValues - 1)) >> 1) - ((offset * (offset - 1)) >> 1)); it doesn't agree with my intuition, e.g. changing the value of offset from 0 to 1 would not change x at all?? :)

              Luc Pattyn [My Articles] Nil Volentibus Arduum

              A Offline
              A Offline
              A
              wrote on last edited by
              #6

              Thanks Luc. I was so caught up with optimising that I missed the case where offset was one will have to handle that. Good job spotting this.

              My blog:[^]

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups