make it faster

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Simply_Red

    make it faster

    Hi,

    is there a way to make this function faster???

    struct Points {
    double X;
    double Y;
    };


    double Dist(Points First, Points Last)
    {
    return pow( (First.X-Last.X)*(First. X-Last.X) + (First.Y -
    Last.Y)*(First. Y - Last.Y) ,0.5);
    }
  • Kira Yamato

    #2
    Re: make it faster

    On 2008-02-14 13:10:53 -0500, Simply_Red <Simply.Red75@g mail.comsaid:
    Hi,
    >
    is there a way to make this function faster???
    >
    struct Points {
    double X;
    double Y;
    };
    >
    >
    double Dist(Points First, Points Last)
    {
    return pow( (First.X-Last.X)*(First. X-Last.X) + (First.Y -
    Last.Y)*(First. Y - Last.Y) ,0.5);
    }
    google up 'fastsqrt' and see if it helps.

    --

    // kira

    Comment

    • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

      #3
      Re: make it faster

      On 2008-02-14 19:10, Simply_Red wrote:
      Hi,
      >
      is there a way to make this function faster???
      >
      struct Points {
      double X;
      double Y;
      };
      >
      >
      double Dist(Points First, Points Last)
      {
      return pow( (First.X-Last.X)*(First. X-Last.X) + (First.Y -
      Last.Y)*(First. Y - Last.Y) ,0.5);
      }
      You could try to use the pow() instead of multiplying and using sqrt()
      instead of pow(x, 0.5).

      But more importantly, are you sure that Dist() is the function you need
      to make faster?

      --
      Erik Wikström

      Comment

      • Simply_Red

        #4
        Re: make it faster

        On 14 fév, 13:22, Erik Wikström <Erik-wikst...@telia. comwrote:
        On 2008-02-14 19:10, Simply_Red wrote:
        >
        Hi,
        >
        is there a way to make this function faster???
        >
        struct Points {
        double X;
        double Y;
        };
        >
        double Dist(Points First, Points Last)
        {
        return pow( (First.X-Last.X)*(First. X-Last.X) + (First.Y -
        Last.Y)*(First. Y - Last.Y) ,0.5);
        }
        >
        You could try to use the pow() instead of multiplying and using sqrt()
        instead of pow(x, 0.5).
        >
        But more importantly, are you sure that Dist() is the function you need
        to make faster?
        >
        --
        Erik Wikström

        I did a test and multiplying is faster than pow,
        and what is strange, pow(x,0.5) is faster than sqrt(x).


        I have to make my program working faster, so i try to find where it is
        possible.

        Comment

        • Michael Angelo Ravera

          #5
          Re: make it faster

          On Feb 14, 10:10 am, Simply_Red <Simply.Re...@g mail.comwrote:
          Hi,
          >
          is there a way to make this function faster???
          >
          struct Points {
                  double X;
                  double Y;
          >
          };
          >
          double Dist(Points First, Points Last)
          {
              return pow( (First.X-Last.X)*(First. X-Last.X) + (First.Y -
          Last.Y)*(First. Y - Last.Y) ,0.5);
          >
          >
          >
          }- Hide quoted text -
          >
          - Show quoted text -
          Several ways:
          1) sqrt is likely to be faster than pow
          2) a #define is likely to be faster that a member function
          3) most high level optimizers will compute the common subexpressions
          only once, but maybe you could help it by

          x_diff = First.X-Last.X;
          x_diff *= x_diff;

          y_diff = First.Y-Last.Y;
          y_diff *= y_diff;

          x_diff += y_diff;

          return sqrt (x_diff);

          You may be able to help it even more, if x_diff and y_diff can be kept
          in registers.

          Comment

          • Victor Bazarov

            #6
            Re: make it faster

            Michael Angelo Ravera wrote:
            On Feb 14, 10:10 am, Simply_Red <Simply.Re...@g mail.comwrote:
            >Hi,
            >>
            >is there a way to make this function faster???
            >>
            >struct Points {
            >double X;
            >double Y;
            >>
            >};
            >>
            >double Dist(Points First, Points Last)
            >{
            >return pow( (First.X-Last.X)*(First. X-Last.X) + (First.Y -
            >Last.Y)*(First .Y - Last.Y) ,0.5);
            >>
            >>
            >>
            >}- Hide quoted text -
            >>
            >- Show quoted text -
            >
            Several ways:
            1) sqrt is likely to be faster than pow
            2) a #define is likely to be faster that a member function
            3) most high level optimizers will compute the common subexpressions
            only once, but maybe you could help it by
            >
            x_diff = First.X-Last.X;
            x_diff *= x_diff;
            >
            y_diff = First.Y-Last.Y;
            y_diff *= y_diff;
            >
            x_diff += y_diff;
            >
            return sqrt (x_diff);
            >
            You may be able to help it even more, if x_diff and y_diff can be kept
            in registers.
            There is also a POSIX function _hypot, I believe. So you could try
            doing

            double Dist(Points const& First, Points const& Last)
            {
            return _hypot( First.X - Last.X, First.Y - Last.Y );
            }

            Note the use of 'const&' for arguments. Not sure if it's better, so
            do test, it might be, on your system.

            V
            --
            Please remove capital 'A's when replying by e-mail
            I do not respond to top-posted replies, please don't ask


            Comment

            • Simply_Red

              #7
              Re: make it faster

              On 14 fév, 14:05, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
              Michael Angelo Ravera wrote:
              On Feb 14, 10:10 am, Simply_Red <Simply.Re...@g mail.comwrote:
              Hi,
              >
              is there a way to make this function faster???
              >
              struct Points {
              double X;
              double Y;
              >
              };
              >
              double Dist(Points First, Points Last)
              {
              return pow( (First.X-Last.X)*(First. X-Last.X) + (First.Y -
              Last.Y)*(First. Y - Last.Y) ,0.5);
              >
              }- Hide quoted text -
              >
              - Show quoted text -
              >
              Several ways:
              1) sqrt is likely to be faster than pow
              2) a #define is likely to be faster that a member function
              3) most high level optimizers will compute the common subexpressions
              only once, but maybe you could help it by
              >
              x_diff = First.X-Last.X;
              x_diff *= x_diff;
              >
              y_diff = First.Y-Last.Y;
              y_diff *= y_diff;
              >
              x_diff += y_diff;
              >
              return sqrt (x_diff);
              >
              You may be able to help it even more, if x_diff and y_diff can be kept
              in registers.
              >
              There is also a POSIX function _hypot, I believe. So you could try
              doing
              >
              double Dist(Points const& First, Points const& Last)
              {
              return _hypot( First.X - Last.X, First.Y - Last.Y );
              }
              >
              Note the use of 'const&' for arguments. Not sure if it's better, so
              do test, it might be, on your system.
              >
              V
              --
              Please remove capital 'A's when replying by e-mail
              I do not respond to top-posted replies, please don't ask
              after a test, it's not better.

              Comment

              • Simply_Red

                #8
                Re: make it faster

                On 14 fév, 13:10, Simply_Red <Simply.Re...@g mail.comwrote:
                Hi,
                >
                is there a way to make this function faster???
                >
                struct Points {
                double X;
                double Y;
                >
                };
                >
                double Dist(Points First, Points Last)
                {
                return pow( (First.X-Last.X)*(First. X-Last.X) + (First.Y -
                Last.Y)*(First. Y - Last.Y) ,0.5);
                >
                }
                Dist is used at least 497235 time, so even a small change can make a
                difference.

                Comment

                • Victor Bazarov

                  #9
                  Re: make it faster

                  Simply_Red wrote:
                  On 14 fév, 14:05, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
                  >Michael Angelo Ravera wrote:
                  >>On Feb 14, 10:10 am, Simply_Red <Simply.Re...@g mail.comwrote:
                  >>>Hi,
                  >>
                  >>>is there a way to make this function faster???
                  >>
                  >>>struct Points {
                  >>>double X;
                  >>>double Y;
                  >>
                  >>>};
                  >>
                  >>>double Dist(Points First, Points Last)
                  >>>{
                  >>>return pow( (First.X-Last.X)*(First. X-Last.X) + (First.Y -
                  >>>Last.Y)*(Fir st.Y - Last.Y) ,0.5);
                  >>
                  >>>}- Hide quoted text -
                  >>
                  >>>- Show quoted text -
                  >>
                  >>Several ways:
                  >>1) sqrt is likely to be faster than pow
                  >>2) a #define is likely to be faster that a member function
                  >>3) most high level optimizers will compute the common subexpressions
                  >>only once, but maybe you could help it by
                  >>
                  >>x_diff = First.X-Last.X;
                  >>x_diff *= x_diff;
                  >>
                  >>y_diff = First.Y-Last.Y;
                  >>y_diff *= y_diff;
                  >>
                  >>x_diff += y_diff;
                  >>
                  >>return sqrt (x_diff);
                  >>
                  >>You may be able to help it even more, if x_diff and y_diff can be
                  >>kept in registers.
                  >>
                  >There is also a POSIX function _hypot, I believe. So you could try
                  >doing
                  >>
                  > double Dist(Points const& First, Points const& Last)
                  > {
                  > return _hypot( First.X - Last.X, First.Y - Last.Y );
                  > }
                  >>
                  >Note the use of 'const&' for arguments. Not sure if it's better, so
                  >do test, it might be, on your system.
                  >>
                  >V
                  >--
                  >Please remove capital 'A's when replying by e-mail
                  >I do not respond to top-posted replies, please don't ask
                  >
                  after a test, it's not better.
                  Which suggests that your guess is not good. You're probably trying
                  to improve the part of the program that is not the culprit. Get
                  yourself a profiler and measure!

                  V
                  --
                  Please remove capital 'A's when replying by e-mail
                  I do not respond to top-posted replies, please don't ask


                  Comment

                  • Default User

                    #10
                    Re: make it faster

                    Simply_Red wrote:
                    On 14 fev, 14:08, Pete Becker <p...@versatile coding.comwrote :
                    On 2008-02-14 13:50:20 -0500, Simply_Red <Simply.Re...@g mail.com>
                    said:


                    I have to make my program working faster, so i try to find where
                    it is possible.
                    If your program is too slow, you have to speed up the parts that are
                    making it slow.
                    Sometimes a small detail can make a code faster, i'm searching for all
                    math in my code and try to find if it can be faster.

                    You have no way of knowing whether your efforts help, hinder, or make
                    no difference. If you are concerned about speed, get a profiler and
                    figure out where the problems are.




                    Brian

                    Comment

                    • Victor Bazarov

                      #11
                      Re: make it faster

                      Simply_Red wrote:
                      On 14 fév, 13:10, Simply_Red <Simply.Re...@g mail.comwrote:
                      >Hi,
                      >>
                      >is there a way to make this function faster???
                      >>
                      >struct Points {
                      > double X;
                      > double Y;
                      >>
                      >};
                      >>
                      >double Dist(Points First, Points Last)
                      >{
                      > return pow( (First.X-Last.X)*(First. X-Last.X) + (First.Y -
                      >Last.Y)*(First .Y - Last.Y) ,0.5);
                      >>
                      >}
                      >
                      Dist is used at least 497235 time, so even a small change can make a
                      difference.
                      Not if those half-a-million times end up being 0.1% of the overall
                      run time.

                      V
                      --
                      Please remove capital 'A's when replying by e-mail
                      I do not respond to top-posted replies, please don't ask


                      Comment

                      • James Kanze

                        #12
                        Re: make it faster

                        On Feb 14, 8:57 pm, Simply_Red <Simply.Re...@g mail.comwrote:
                        On 14 fév, 13:10, Simply_Red <Simply.Re...@g mail.comwrote:
                        is there a way to make this function faster???
                        struct Points {
                        double X;
                        double Y;
                        };
                        double Dist(Points First, Points Last)
                        {
                        return pow( (First.X-Last.X)*(First. X-Last.X) + (First.Y -
                        Last.Y)*(First. Y - Last.Y) ,0.5);
                        }
                        Dist is used at least 497235 time, so even a small change can
                        make a difference.
                        How much time does Dist actually take? (A quick check on my
                        machine says less than 50 nanoseconds. Which means that a half
                        a million times is still something to be measured in 10's of
                        milliseconds---hardly a problem, I would think.)

                        --
                        James Kanze (GABI Software) email:james.kan ze@gmail.com
                        Conseils en informatique orientée objet/
                        Beratung in objektorientier ter Datenverarbeitu ng
                        9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                        Comment

                        • Simply_Red

                          #13
                          Re: make it faster

                          i found another function that take much more time

                          with this version i win 40% of time:

                          Points func_faster(Poi nts* pt,double mu)
                          {
                          double mum1, mum13, mu3,mumCarre,mu Carre;
                          Points p;
                          mum1 = 1 - mu;
                          mumCarre = mum1 * mum1;
                          mum13 = mumCarre * mum1;
                          muCarre = mu * mu;
                          mu3 = muCarre * mu;
                          double tempValue1 = 3 * mu * mumCarre;
                          double tempValue2 = 3 * muCarre * mum1;
                          p.X = mum13 * pt[0].X + tempValue1 * pt[1].X + tempValue2 * pt[2].X
                          + mu3 * pt[3].X;
                          p.Y = mum13 * pt[0].Y + tempValue1 * pt[1].Y + tempValue2 * pt[2].Y
                          + mu3 * pt[3].Y;
                          return p;
                          }


                          Points func(Points* pt,double mu)
                          {
                          double mum1, mum13, mu3;
                          Points p;
                          mum1 = 1 - mu;
                          mum13 = mum1 * mum1 * mum1;
                          mu3 = mu * mu * mu;

                          p.X = mum13 * pt[0].X + 3 * mu * mum1 * mum1 * pt[1].X + 3 * mu *
                          mu * mum1 * pt[2].X + mu3 * pt[3].X;
                          p.Y = mum13 * pt[0].Y + 3 * mu * mum1 * mum1 * pt[1].Y + 3 * mu *
                          mu * mum1 * pt[2].Y + mu3 * pt[3].Y;
                          return p;
                          }

                          Comment

                          • Default User

                            #14
                            Re: make it faster

                            James Kanze wrote:
                            On Feb 14, 8:57 pm, Simply_Red <Simply.Re...@g mail.comwrote:
                            Dist is used at least 497235 time, so even a small change can
                            make a difference.
                            >
                            How much time does Dist actually take? (A quick check on my
                            machine says less than 50 nanoseconds. Which means that a half
                            a million times is still something to be measured in 10's of
                            milliseconds---hardly a problem, I would think.)
                            This is exactly the problem with shotgun optimizations. One can spend a
                            lot of time, and obfuscate the code, for essentially no return.
                            Sometimes the efforts will actually slow things down, when you defeat a
                            built-in compiler optimization.



                            Brian

                            Comment

                            Working...