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 / C++ / MFC
  4. Efficiency question

Efficiency question

Scheduled Pinned Locked Moved C / C++ / MFC
question
5 Posts 5 Posters 1 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.
  • Y Offline
    Y Offline
    yellowine
    wrote on last edited by
    #1

    I have the following codes:

    double Length0(double x1, double y1, double z1, double x2, double y2, double z2)
    {
    double dx = x2 - x1;
    double dy = y2 - y1;
    double dz = z2 - z1;

    return (dx\*dx + dy\*dy + dz\*dz);
    

    }

    double Length1(double x1, double y1, double z1, double x2, double y2, double z2)
    {
    return ((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1) + (z2 - z1)*(z2 - z1));
    }

    Assume all other conditions are the same, which one (Length0 or Length1) theoretically executes faster? A bit explanation is highly appreciated.

    S M _ E 4 Replies Last reply
    0
    • Y yellowine

      I have the following codes:

      double Length0(double x1, double y1, double z1, double x2, double y2, double z2)
      {
      double dx = x2 - x1;
      double dy = y2 - y1;
      double dz = z2 - z1;

      return (dx\*dx + dy\*dy + dz\*dz);
      

      }

      double Length1(double x1, double y1, double z1, double x2, double y2, double z2)
      {
      return ((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1) + (z2 - z1)*(z2 - z1));
      }

      Assume all other conditions are the same, which one (Length0 or Length1) theoretically executes faster? A bit explanation is highly appreciated.

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      With optimisations on, both the same - common sub-expression elimination should reduce Length1 to the equivalent of Length0, except with the variables in registers rather than on the stack. With optimisations off...probably Length0 is more efficient.

      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

      1 Reply Last reply
      0
      • Y yellowine

        I have the following codes:

        double Length0(double x1, double y1, double z1, double x2, double y2, double z2)
        {
        double dx = x2 - x1;
        double dy = y2 - y1;
        double dz = z2 - z1;

        return (dx\*dx + dy\*dy + dz\*dz);
        

        }

        double Length1(double x1, double y1, double z1, double x2, double y2, double z2)
        {
        return ((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1) + (z2 - z1)*(z2 - z1));
        }

        Assume all other conditions are the same, which one (Length0 or Length1) theoretically executes faster? A bit explanation is highly appreciated.

        M Offline
        M Offline
        Maximilien
        wrote on last edited by
        #3

        probably negligible with modern compilers.

        This signature was proudly tested on animals.

        1 Reply Last reply
        0
        • Y yellowine

          I have the following codes:

          double Length0(double x1, double y1, double z1, double x2, double y2, double z2)
          {
          double dx = x2 - x1;
          double dy = y2 - y1;
          double dz = z2 - z1;

          return (dx\*dx + dy\*dy + dz\*dz);
          

          }

          double Length1(double x1, double y1, double z1, double x2, double y2, double z2)
          {
          return ((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1) + (z2 - z1)*(z2 - z1));
          }

          Assume all other conditions are the same, which one (Length0 or Length1) theoretically executes faster? A bit explanation is highly appreciated.

          _ Offline
          _ Offline
          _Superman_
          wrote on last edited by
          #4

          Below is the assembly code generated by Visual Studio 2008 for both the functions. The code was generated with full optimization.

          ?Length0@@YANNNNNNN@Z PROC

          fld	QWORD PTR \_x2$\[esp-4\]
          fsub	QWORD PTR \_x1$\[esp-4\]
          fld	QWORD PTR \_y2$\[esp-4\]
          fsub	QWORD PTR \_y1$\[esp-4\]
          fld	QWORD PTR \_z2$\[esp-4\]
          fsub	QWORD PTR \_z1$\[esp-4\]
          fld	ST(1)
          fmulp	ST(2), ST(0)
          fld	ST(2)
          fmulp	ST(3), ST(0)
          fxch	ST(1)
          faddp	ST(2), ST(0)
          fmul	ST(0), ST(0)
          faddp	ST(1), ST(0)
          
          ret	0
          

          ?Length0@@YANNNNNNN@Z ENDP

          ?Length1@@YANNNNNNN@Z PROC

          fld	QWORD PTR \_x2$\[esp-4\]
          fsub	QWORD PTR \_x1$\[esp-4\]
          fld	QWORD PTR \_y2$\[esp-4\]
          fsub	QWORD PTR \_y1$\[esp-4\]
          fld	QWORD PTR \_z2$\[esp-4\]
          fsub	QWORD PTR \_z1$\[esp-4\]
          fld	ST(1)
          fmulp	ST(2), ST(0)
          fld	ST(2)
          fmulp	ST(3), ST(0)
          fxch	ST(1)
          faddp	ST(2), ST(0)
          fmul	ST(0), ST(0)
          faddp	ST(1), ST(0)
          
          ret	0
          

          ?Length1@@YANNNNNNN@Z ENDP

          Here you will notice that both are exactly the same. The compiler has taken away the responsibility from the programmer about optimizing code. At least a lot of it. :)

          «_Superman_» I love work. It gives me something to do between weekends.

          1 Reply Last reply
          0
          • Y yellowine

            I have the following codes:

            double Length0(double x1, double y1, double z1, double x2, double y2, double z2)
            {
            double dx = x2 - x1;
            double dy = y2 - y1;
            double dz = z2 - z1;

            return (dx\*dx + dy\*dy + dz\*dz);
            

            }

            double Length1(double x1, double y1, double z1, double x2, double y2, double z2)
            {
            return ((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1) + (z2 - z1)*(z2 - z1));
            }

            Assume all other conditions are the same, which one (Length0 or Length1) theoretically executes faster? A bit explanation is highly appreciated.

            E Offline
            E Offline
            Eytukan
            wrote on last edited by
            #5

            yellowine wrote:

            double Length0(double x1, double y1, double z1, double x2, double y2, double z2) { double dx = x2 - x1; double dy = y2 - y1; double dz = z2 - z1; return (dx*dx + dy*dy + dz*dz); }

            You can see no specific improvements with that change there. but on the other hand There would be some improvements with the first case (length0), If the calculations happen inside a loop. just a rough example:

            double y1, double z1, double x2, double y2, double z2)
            {
            double dx = x2 - x1;
            double dy = y2 - y1;
            double dz = z2 - z1;

            double result =0.0;

            for(int i=xx;i<yx;i++)>
            {
            result += (dx* i + dy * i + dz * i);
            }
            return result;
            }

            There it would make sense to keep them out of the loop and see some performance improvements.

            He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus

            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